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

attempt to index nil with 'Touched' how to fix this ?

guys i want to script a zombie to kill people and I have an error that make it doesnt damage players please help me there is the script:

local rarm = script.Parent:FindFirstChild("Right Arm")
local larm = script.Parent:FindFirstChild("Left Arm")

function dmg(hit)
	if hit.Parent ~= nil then
		local hum = hit.Parent:findFirstChild("Humanoid")
		if hum ~= nil then
			hum.Health = hum.Health -10
		end
	end
end

rarm.Touched:connect(dmg)
larm.Touched:connect(dmg)

2 comments

0

You use R6 character version?

0

Just add brackets when you call dmg, and pass through the hit object.

2 answers

You have created a function that requires a parameter, but you are calling it without giving it the required parameter.

Try this:

local rarm = script.Parent:FindFirstChild("Right Arm")
local larm = script.Parent:FindFirstChild("Left Arm")
	 
rarm.Touched:connect(function(Hit) -- So I can get the parameter(Hit)
	if hit.Parent ~= nil then
	local hum = hit.Parent:findFirstChild("Humanoid")
		if hum ~= nil then
			hum.Health = hum.Health -10
		end
	end
end)

larm.Touched:connect(function(Hit) -- So I can get the parameter(Hit)
	if hit.Parent ~= nil then
	local hum = hit.Parent:findFirstChild("Humanoid")
		if hum ~= nil then
			hum.Health = hum.Health -10
		end
	end
end)
local rarm = script.Parent:WaitForChild("Right Arm") -- WaitForChild is waiting for that object until its loaded, you problem was that "rarm" or "larm" doesnt exist
local larm = script.Parent:WaitForChild("Left Arm")

function dmg(hit)
    if hit.Parent ~= nil then
        local hum = hit.Parent:findFirstChild("Humanoid")
        if hum ~= nil then
            hum.Health = hum.Health -10
        end
    end
end

rarm.Touched:connect(dmg)
larm.Touched:connect(dmg)

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