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

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

Asked by 3 years ago
Edited by JesseSong 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

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)

0
You use R6 character version? NiniBlackJackQc 1562 — 3y
0
Just add brackets when you call dmg, and pass through the hit object. thecelestialcube 123 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
Ad
Log in to vote
0
Answered by 3 years ago
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)

Answer this question