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

Why is this code giving me an error?

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

✓ Accepted answer

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

2 comments

0

Thanks.

0

Remember to upvote positive answers and also mark an accepted answer for future visitors..

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)

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