Search code examples
luaroblox

Roblox studio error attempt to call a nil value


My code is this

player = game.Players.LocalPlayer
while game.Players.LocalPlayer == nil do
    player = game.Players.LocalPlayer
    wait(0.1)
end


local mouse = player:GetMouse()


mouse.Button1Up:Connect(addpoints())

function addpoints()
    print("add points")
end

this is a local script in a tool and the tool is in the starter pack. and on the line that says ```mouse.Button1Up`` i get an error attempt to call a nil value but i know from using breakpoints that mouse isn't nil


Solution

  • Your issue is that the addPoints fuction doesn't exist by the time you are trying to connect it.

    So to fix your problem, 1) move the function definition above the connection, and 2) remove the parentheses so that you are passing the function itself.

    function addpoints()
        print("add points")
    end
    
    mouse.Button1Up:Connect(addpoints)