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 4 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

local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()

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

1 answer

Log in to vote
1
Answered by 4 years ago

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

local playerServ = game:GetService("Players") -- Get the players service
local player = playerServ.LocalPlayer -- Use the LocalPlayer property
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
    local kaboom = Instance.new("Explosion",workspace)
    kaboom.Position = mouse.Hit.Position
end)
Ad

Answer this question