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 'Players.(PlayerName):WaitForChild("Character")? How to fix?

I was trying to make a item that can attack a player, but whenever I try to get character, it puts an infinite yield. I've been trying and trying, and it does not work.

I tried

local Character = player.Character or player.CharacterAdded:wait()

Doesn't work.

This is literally my whole script.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:wait()
local Idle = player:WaitForChild("Character"):WaitForChild("Humanoid"):LoadANimation(script:WaitForChild("Idle"))
local Attack1 = player:WaitForChild("Character"):WaitForChild("Humanoid"):LoadANimation(script:WaitForChild("Attack1"))
local Attack2 = player:WaitForChild("Character"):WaitForChild("Humanoid"):LoadANimation(script:WaitForChild("Attack2"))
local num = 1

script.Parent.Equipped:Connect(function()
	Idle:Play()
end)

script.Parent.Equipped:Connect(function()
	Idle:Stop()
end)


script.Parent.Activated:Connect(function()
	if num == 1 then
		Attack1:Play()
		script.Parent.RemoteEvent:FireServer()
	else
		Attack2:Play()
		script.Parent.RemoteEvent:FireServer()
	end
end)

Can anybody help me figure this out?

5 comments

0

On your Animations variables you do player:WaitForChild("Character") when you should only do Character

0

The Character Rig was already allocated, there is no need to yield for it once again. Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(AnimObject)

0

Isn't that what I said?

0

Where would I put Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(AnimObject) anyway?

0

Your attempt to reference the Character worked, that is the recommended practice to allocate it in a variable. It's the code below the statement that is failing.

2 answers

✓ Accepted answer

The Character Object is not a child of the Player Object. It's affiliated avatar rig is actually located in workspace, whilst the Client is under a container named Players; this is why Lua is having a hard time finding it, since :WaitForChild() looks for the given Instance as a child of the Object the method is called on. ROBLOX automatically stores a reference property to the Character Rig, you can reference it properly via .Character.

You've also already allocated the avatar, meaning there is no reason to continue to yield your script for it, simply just use the Character variable already declared

Note:

You also misspelled :LoadAnimation(), which will raise a nil method exception.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Idle = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
local Attack1 = Humanoid:LoadAnimation(script:WaitForChild("Attack1"))
local Attack2 = Humanoid:LoadAnimation(script:WaitForChild("Attack2"))
local Number = 1

local Tool = script.Parent

Tool.Equipped:Connect(function()
    Idle:Play()
end)

Tool.Equipped:Connect(function()
    Idle:Stop()
end)


Tool.Activated:Connect(function()
    if (Number == 1) then
        Attack1:Play()
        Tool.RemoteEvent:FireServer()
    else
        Attack2:Play()
        Tool.RemoteEvent:FireServer()
    end
end)

8 comments

0

So I reference it by workspace.Character?

0

As I said, you've already declared the Player's Character; you already have access to it at line 2, just call Character to modify the avatar rig.

0

16:37:32.375 - Players.bowslonely.Backpack.Cub.AnimationScript:4: attempt to index local 'Humanoid' (a nil value)

0

Try the code once more. I edited it.

0

Alright, whenever I did that it only worked once, and it stopped working.

0

It will only work until you reset the Character, since the avatar rig's allocation become no-longer valid since the Character is erased and another is loaded as a replacement. I solved your issue, this is something that you'll have to fix on your own.

0

I did fix it myself, but I dont know why the idle part isnt playing.

0

Because you told it to stop. Read the logic on 15-17

workspace:WaitForChild(plr.name)

Actually, your problem is the :LoadANimation part. but also, you can't do the :WaitForChild() for the player's character, since it's not an actual descendant of the player instance.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:wait()
local Idle = player:WaitForChild("Character"):WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("Idle"))
local Attack1 = player:WaitForChild("Character"):WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("Attack1"))
local Attack2 = player:WaitForChild("Character"):WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("Attack2"))
local num = 1

script.Parent.Equipped:Connect(function()
    Idle:Play()
end)

script.Parent.Equipped:Connect(function()
    Idle:Stop()
end)


script.Parent.Activated:Connect(function()
    if num == 1 then
        Attack1:Play()
        script.Parent.RemoteEvent:FireServer()
    else
        Attack2:Play()
        script.Parent.RemoteEvent:FireServer()
    end
end)

Just a typo. :)

3 comments

0

No

0

The error doesn't lie. If he gets an error that says it's something wrong with player:WaitForChild("Character") then you can't say that the problem is actually in :LoadAnimation()

0

That makes no sense at all honestly, it's literally a yield possibility, and literally there is no grammar error on that, why does that even matter?

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