Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Yeevivor4 155
10 years ago
 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?

0
Make sure you're using a localscript, the GetMouse method doesn't work with a server side script. Spongocardo 1991 — 10y

1 answer

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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)
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. Yeevivor4 155 — 10y
Ad

Answer this question