player = game.Players:GetChildren() for i = 1, #player do if player[i] ~= nil then mouse = player:GetMouse() PartsLeft = workspace.PartsLeft function addToParts() if mouse.Target == script.Parent then PartsLeft.Value = PartsLeft.Value + 1 end end end end mouse.Button1Down:connect(addToParts)
Whenever I go into the game and press on this part, it does not add to the PartsLeft Value. It says Attempt to index call method "GetMouse" (a nil value)
. Did I not add something?
From what I see, this is a server script. GetMouse()
does not work in server scripts. But don't just change it all to a local script because local scripts will only run in PlayerGui or Backpack, and it looks to me that you have this in a brick.
This is pretty easy to accomplish with a server script, though, because there is an Instance named ClickDetector
that, when placed inside a brick, will allow you to fire MouseClick events from the brick.
Put a ClickDetector inside your brick, then try this code:
local PartsLeft = workspace:WaitForChild("PartsLeft") --In case it has not loaded yet. function addToParts() PartsLeft.Value = PartsLeft.Value + 1 end script.Parent.ClickDetector.MouseClick:connect(addToParts)