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

01local db = false --debounce
02 
03script.Parent.Equipped:Connect(function() --when its equipped
04 
05    local player = game.Players:FindFirstChild( script.Parent.Parent.Name) --player
06local character = player.Character --players character
07local Handle = script.Parent.Handle --the tools handle
08        local Fire = script.Parent.Particles.FirePart.Fire --the particle for when you fire the gun
09 
10script.Parent.Activated:Connect(function() --when clicked
11    if db == false then --debounce
12        db = true
13local mouse = player:GetMouse()
14        print(mouse.Target.Name) --error
15 
View all 22 lines...
0
Is it a server script? Raccoonyz 1092 — 4y
0
You didn't ever define mouse. Utter_Incompetence 856 — 4y
0
He did, are you blind? Raccoonyz 1092 — 4y
0
i define the mouse right before the error and yeah its in a server script bx3t 2 — 4y
0
anyone know how to fix it bx3t 2 — 4y

1 answer

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

Add a check if Target isn't nil

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

Answer this question