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

Why is this code giving me an error?

Asked by 10 years ago
game.Players.PlayerAdded:connect(function(player)
    mouse = player:GetMouse()
    mouse.KeyDown:connect(function()
        print("Success")
    end)
end)

Why do I get the error:

error trying to index global 'mouse' (a nil value)

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

GetMouse only works from a LocalScript, so it wouldn't work in general using the PlayerAdded event.

0
Thanks. GoFigureYouPurple 10 — 10y
0
Remember to upvote positive answers and also mark an accepted answer for future visitors.. BlueTaslem 18071 — 10y
Ad
Log in to vote
-1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago

Mouse is client side issue, meaning you must use a Local script in order to use this script. Place a LocalScript in StarterGui or StarterPack, and place this code;

plr =script.Parent.Parent
mouse=plr:GetMouse()
mouse.KeyDown:connect(function()
print("Success")
end)

Answer this question