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

"Script:14: attempt to index nil with 'Target'" How do I fix this?

Asked by
bx3t 2
3 years ago

I'm trying to make a gun right now. When ever i click it just leaves me with this error: attempt to index nil with 'Target'"

local db = false --debounce

script.Parent.Equipped:Connect(function() --when its equipped

    local player = game.Players:FindFirstChild( script.Parent.Parent.Name) --player
local character = player.Character --players character
local Handle = script.Parent.Handle --the tools handle
        local Fire = script.Parent.Particles.FirePart.Fire --the particle for when you fire the gun

script.Parent.Activated:Connect(function() --when clicked
    if db == false then --debounce
        db = true
local mouse = player:GetMouse()
        print(mouse.Target.Name) --error

                                --still writing some code here

        wait(1)
        db = false --setting the debounce back to false after 1 second
    end
end)
end)
0
Is it a server script? Raccoonyz 1092 — 3y
0
You didn't ever define mouse. Utter_Incompetence 856 — 3y
0
He did, are you blind? Raccoonyz 1092 — 3y
0
i define the mouse right before the error and yeah its in a server script bx3t 2 — 3y
0
anyone know how to fix it bx3t 2 — 3y

1 answer

Log in to vote
0
Answered by
zuup123 99
3 years ago

Add a check if Target isn't nil

if mouse.Target ~= nil then
print(mouse.Target.Name)
end
Ad

Answer this question