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

Infinite yield possible on 'ServerStorage:WaitForChild("zombieradio") help me please ?

Asked by 4 years ago

I have the problem with the attack of my zombie can you help me please here is the script:

01local rarm = script.Parent:WaitForChild("Right Arm")
02    local larm = script.Parent:WaitForChild("Left Arm")
03 
04    function 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
11        end
12 
13    rarm.Touched:connect(dmg)
14    larm.Touched:connect(dmg)

Thanks

0
I don't see zombieradio anywhere Valkyrie1277 28 — 4y

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago

The function <Object>:WaitForChild(<thing>,<timeout>) as you can see it has two arguments One for the name of the object you're looking for and a timeout (in seconds). So if you have no timeout in your script's it will hang for a while until the timeout error Infinite yield is thrown.

The proper way to use the function is as follows:

--Wrong way (kinda)

01local rarm = script.Parent:WaitForChild("Right Arm") -- this will hang the script if it cannot find an object named "Right Arm", Though if it finds it then it will continue as normal!
02 
03local larm = script.Parent:WaitForChild("Left Arm") -- this will hang the script if it cannot find an object named "Left Arm", Though if it finds it then it will continue as normal!
04 
05    function dmg(hit)
06            if hit.Parent ~= nil then
07                    local hum = hit.Parent:findFirstChild("Humanoid")
08                    if hum ~= nil then
09                            hum.Health = hum.Health -10
10                        end
11                end
12        end
13 
14    rarm.Touched:connect(dmg)
15    larm.Touched:connect(dmg)

Correct Way:

01local rarm = script.Parent:WaitForChild("Right Arm",10) -- this will suspend the script until the object("Right Arm") appears in script.Parent. If it doesn't find it after 10 secs it will make this equal nil
02 
03local larm = script.Parent:WaitForChild("Left Arm",10-- this will suspend the script until the object("Left Arm") appears in script.Parent. If it doesn't find it after 10 secs it will make this equal nil
04    function 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
11        end
12--// if any of the above checks come back nil, this table will be filled with nil's and since you cannot have a nil property of a table the length of the table will be less than the number of checks!
13local ChkTable = {rarm,larm}
14if(#ChkTable < 2) then
15    if(rarm == nil) then    warn("Error: Right Arm is nil!") end
16    if(larm == nil) then    warn("Error: Left Arm is nil!") end
17    error("Error: Something went wrong with WaitForChild() Checks as not all objects are found in the check table!")
18end
19    rarm.Touched:connect(dmg)
20    larm.Touched:connect(dmg)

ask if your unsure on anything and Hope this helps! :)

Ad

Answer this question