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

Infinite yield possible on 'Players.Player1:WaitForChild("Character")'? any help

Asked by 7 years ago

this is the error Infinite yield possible on 'Players.Player1:WaitForChild("Character")'

here's a part of the script

game.Players.LocalPlayer:WaitForChild("leaderstats") game.Players.LocalPlayer.leaderstats:WaitForChild("Level") game.StarterGui:WaitForChild("Hp") game.StarterGui.Hp.Frame.Frame:WaitForChild("HpPart") game.StarterGui.Hp.Lv:WaitForChild("LVL DISPLAY SCRIPT")

local player = game.Players.LocalPlayer player:WaitForChild("Character") local Character = player.Character

if game.Players.LocalPlayer.leaderstats.Level.Value == 1 then player.Character.Humanoid.MaxHealth = 250 player.Character.Humanoid.Health = 250 end

the problem is in this part

local player = game.Players.LocalPlayer player:WaitForChild("Character") local Character = player.Character

i can't figure it out? any help ?

0
Players.Player1:WaitForChild("Character") will always return nil, since Character is not a Child, its a redirection. RubenKan 3615 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

Essentially all this means is that ROBLOX is warning you that your script can potentially wait forever, if the object you set to wait for hasn't been made.

You shouldn't have a problem with it (and it is just a warning, not an error), but if you want to get rid of the error, set a time where if the object is not created, the WaitForChild will just return nil and end. Ex:

game.Workspace:WaitForChild("Part", 100) -- if after 100 seconds there is no part in the workspace, this function will return nil.

0
thank you so much! TigerClaws454 48 — 7y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

In your following script,

local player = game.Players.LocalPlayer player:WaitForChild("Character") local Character = player.Character

Remove the part
player:WaitForChild("Character")

The correct script becomes
local player = game.Players.LocalPlayer
local Character = player.Character

Character is not child of player although we can write player.Character.
player:WaitForChild("Character") will always result infinite yield since player will never get a child named Character.

if you want to make sure player.Character exists, use
local Character = player.Character or player.CharacterAdded:wait()

Answer this question