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 4 years ago
Edited by JesseSong 4 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:

01local rarm = script.Parent:FindFirstChild("Right Arm")
02local larm = script.Parent:FindFirstChild("Left Arm")
03 
04function dmg(hit)
05    if hit.Parent ~= nil then
06        local hum = hit.Parent:findFirstChild("Humanoid")
07        if hum ~= nil then
08            hum.Health = hum.Health -10
09        end
10    end
11end
12 
13rarm.Touched:connect(dmg)
14larm.Touched:connect(dmg)
0
You use R6 character version? NiniBlackJackQc 1562 — 4y
0
Just add brackets when you call dmg, and pass through the hit object. thecelestialcube 123 — 4y

2 answers

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

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

Try this:

01local rarm = script.Parent:FindFirstChild("Right Arm")
02local larm = script.Parent:FindFirstChild("Left Arm")
03 
04rarm.Touched:connect(function(Hit) -- So I can get the parameter(Hit)
05    if hit.Parent ~= nil then
06    local hum = hit.Parent:findFirstChild("Humanoid")
07        if hum ~= nil then
08            hum.Health = hum.Health -10
09        end
10    end
11end)
12 
13larm.Touched:connect(function(Hit) -- So I can get the parameter(Hit)
14    if hit.Parent ~= nil then
15    local hum = hit.Parent:findFirstChild("Humanoid")
16        if hum ~= nil then
17            hum.Health = hum.Health -10
18        end
19    end
20end)
Ad
Log in to vote
0
Answered by 4 years ago
01local rarm = script.Parent:WaitForChild("Right Arm") -- WaitForChild is waiting for that object until its loaded, you problem was that "rarm" or "larm" doesnt exist
02local larm = script.Parent:WaitForChild("Left Arm")
03 
04function dmg(hit)
05    if hit.Parent ~= nil then
06        local hum = hit.Parent:findFirstChild("Humanoid")
07        if hum ~= nil then
08            hum.Health = hum.Health -10
09        end
10    end
11end
12 
13rarm.Touched:connect(dmg)
14larm.Touched:connect(dmg)

Answer this question