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

Infinite yield possible on 'ReplicatedStorage:WaitForChild("OnDamage")' how i can fix it?

Hi i have a script to do damage and because this error even my animation is not working. Can you guys help me?

ActionSwing(Animation) Local script

Player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local OnTouch = RS:WaitForChild("OnDamage")
game.Workspace:WaitForChild(Player.Name)

Stone = script.Parent

Anim = game.Lighting.Animations.StoneSwing
Track = Player.Character.Humanoid:LoadAnimation(Anim)

Anim = false
Debounce = false
Stone.Activated:Connect(function()
	if Debounce == false then
		Debounce = true
		Track:Play()
		wait(0.3)
		Anim = true
		wait(0.8)
		Anim = false
		wait(0.2)
		Debounce = false
	end
end)

Stone.Unequipped:Connect(function()
	Track:Stop()
end)

OnTouch script

Player = game.Players.LocalPlayer
game.Workspace:WaitForChild()
Stone = game.StarterPack.Stone

local Damage = 10

Anim = game.Lighting.Animations.StoneSwing


Hitted = false
function OnTouch(part)
    if part.Parent:FindFirstChild("Humanoid") ~= nil and part.Parent.Name ~= Player.Name and Anim == true and Hitted == false then
        Hitted = true
        Damage = 10
        Stone.OnDamage:FireServer(Damage, part)
        wait(0.4)
        Hitted = false
    end
end

(ServerScriptService) StoneDamage(Script)

local RS = game:GetService("ReplicatedStorage")
local Remote = RS:WaitForChild("OnDamage")
local stone = game.StarterPack.Stone

Remote.OnServerEvent:Connect(function(Player,Damage,part)
    part.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
end)



1 answer

✓ Accepted answer

:WaitForChild() method requires both: child name (as string) and timeout (in seconds). That is, if your script doesn't find specified child within 5 seconds as written in this dev page: https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

So, in your script you should use:

RS:WaitForChild("OnDamage", timeOut)

where timeOut is time in seconds you want the script to wait.

2 comments

0

How much should i put?

0

The longest you think that it will take for RS.OnDamage to load. Having too big numbers doesn't really make a difference if the child is guaranteed to load. However, if, fore some reason, the child doesn't load at all (doesn't exist), it will take that much time until the script finally errors out. Usually I would put 60 seconds, but very, very large places (in amount of parts) may require more.

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