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

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

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)

5 comments

0

Is it a server script?

0

You didn't ever define mouse.

0

He did, are you blind?

0

i define the mouse right before the error and yeah its in a server script

0

anyone know how to fix it

1 answer

Add a check if Target isn't nil

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

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