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 3 years ago

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

local rarm = script.Parent:WaitForChild("Right Arm")
    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)

Thanks

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

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 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)

local 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!

local 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!

    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)

Correct Way:

local 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

local 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
    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
--// 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!
local ChkTable = {rarm,larm}
if(#ChkTable < 2) then
    if(rarm == nil) then    warn("Error: Right Arm is nil!") end
    if(larm == nil) then    warn("Error: Left Arm is nil!") end
    error("Error: Something went wrong with WaitForChild() Checks as not all objects are found in the check table!")
end
    rarm.Touched:connect(dmg)
    larm.Touched:connect(dmg)

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

Ad

Answer this question