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

ProblemHelp?"Players.confidencebreath.Backpack.M1911.Shooter:140: attempt to index nil with 'Target'

Hi, Im having a problem with my script and it keeps saying this error messege Players.confidencebreath.Backpack.M1911.Shooter:140: attempt to index nil with 'Target' and its not working on how its supposed if anyone can find a solution and which i mean a simple fix i would be grateful heres the code the problem is in green and if anyone could give me a explantion on why is this happening then i reflect on what to do next time without asking

function KeyDownFunctions(key)
	if key == "r" then
		Reload()
	elseif key == "v" then
		if canshoot == true and reloading == false then
			slice()
		end
	elseif key == "f" then
--Main problem local obj = mouse.Target
		if obj ~= nil and obj:findFirstChild("Activate") ~= nil then
			if (obj.Position - Tool.Handle.Position).magnitude <= 5 then
				obj.Activate.Value = game.Players:playerFromCharacter(Tool.Parent)
			end
		end
		if obj ~= nil and obj.Parent ~= nil then
		local h = obj.Parent:FindFirstChild("Humanoid")
			if obj.Parent.Parent ~= nil then
				h = obj.Parent:FindFirstChild("Humanoid") or obj.Parent.Parent:FindFirstChild("Humanoid") 
			end
			if h ~= nil then
				if h.Parent:findFirstChild("Downed") ~= nil and (h.Parent.Torso.Position - Tool.Handle.Position).magnitude <= 5 then
					h.Parent:findFirstChild("Downed").Activate.Value = game.Players:playerFromCharacter(Tool.Parent)
				end
			end
		end
	end
end

im sorry for the confussing title

1 answer

✓ Accepted answer

If you're getting this error, it means the mouse variable isn't defined. Mouse is acquired by calling GetMouse() on the local player, like this.

local mouse = game.Players.LocalPlayer:GetMouse()

Note that this will only work in LocalScripts. The server can't access the local player or the mouse. Hope this helps.

2 comments

0

Ok, thanks I will try it out later

0

Slight problem now when I press f nothing happens

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