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

mouse is nil when its defined 4 lines above?

Asked by 5 years ago

so im having some issues, this script is a regular script in a tool it keeps saying that mouse is a nil value

heres the code that i made

1local player = script.Parent.Parent.Parent
2local mouse = player:GetMouse()
3 
4script.Parent.Activated:Connect(function()
5    local kaboom = Instance.new("Explosion",workspace)
6    kaboom.Position = mouse.Hit.Position
7end)
0
Where is the script located? BennyBoiOriginal 293 — 5y
0
Also is this script local? BennyBoiOriginal 293 — 5y
0
The script has to be local, and you can set player as game.Players.LocalPlayer Shawnyg 4330 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Thanks to @Shawnyg, you get set the player in a LocalScript with the LocalPlayer property in the Players service.

1local playerServ = game:GetService("Players") -- Get the players service
2local player = playerServ.LocalPlayer -- Use the LocalPlayer property
3local mouse = player:GetMouse()
4 
5script.Parent.Activated:Connect(function()
6    local kaboom = Instance.new("Explosion",workspace)
7    kaboom.Position = mouse.Hit.Position
8end)
Ad

Answer this question