Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How come this does not work when I press the part?

 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?

1 comment

0

Make sure you're using a localscript, the GetMouse method doesn't work with a server side script.

1 answer

✓ Accepted answer

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)

1 comment

0

Thank you so much! I can't believe it was that easy to make it work when I had to make myself have a headache to try and figure it out! Thanks a lot again.

This discussion is preserved as it was. New answers, comments, and votes are closed.