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

Trouble using wait for child and optimizing load time on localscripts ?

Hi, im trying to implement :WaitForChild for its primary purpose (probably dont need to explain). However, i have two dilemas. 1. When i call the following code, I get a "Character" == nil --> error " attempt to index field 'Character' (a nil value)"

game:GetService("Players").LocalPlayer.Character:WaitForChild("LeftLowerLeg")
--repeat wait() until game.Players.LocalPlayer.Character.LeftLowerLeg
local feet = game:GetService("Players").LocalPlayer.Character.LeftLowerLeg
print("destroy")
wait(1)
feet:Destroy()

I tried to get past this by adding more wait for childs but i then got an infinite yield warning/error on trying to find the localplayer.

game:GetService("Players"):WaitForChild("LocalPlayer")
game:GetService("Players").LocalPlayer:WaitForChild("Character")

game:GetService("Players").LocalPlayer.Character:WaitForChild("LeftLowerLeg")
--repeat wait() until game.Players.LocalPlayer.Character.LeftLowerLeg
local feet = game:GetService("Players").LocalPlayer.Character.LeftLowerLeg
print("destroy")
wait(1)
feet:Destroy()

How do i fix this? Thanks

2 answers

Update 2: I managed to get it working by messing around with the code and types of wait mechanisms. Here is the finished code.

repeat wait() until game.Players.LocalPlayer.CharacterAdded	

local feet = game:GetService("Players").LocalPlayer.Character.LeftLowerLeg
print("destroy")
wait(3)
feet:Destroy()

Hi Gunt_r,

You could use the following snippet to ensure the script waits until the character is in game:

local player = game.Players.LocalPlayer

--you can try this vvv
repeat wait() until player.Character
local character = player.Character

--or try this vvv 
local character = player.Character or player.CharacterAdded:Wait() 

This will continue to pause the script until the character has spawned in.

8 comments

0

repeat wait() until player.Character Works. local character = player.Character or player.CharacterAdded:Wait() is even better

0

"If your game does not have FilteringEnabled" FE is forced on all games

0

It can be turned off though, right? So if he had turned it off...

0

kinda, you can untick the checkbox for filtering enabled but that won't do anything. it can't be turned off. also, you can delete parts inside your character locally and theyll also delete on the server. not sure if this was intended by roblox or not. 

0

I tried the repeat until thing as well. It doesnt fix the problem. if you look in the code above, you can see it. I commented it out i was testing it earlier.

0

I also havent meddled with filtering enabled. I just checked it again and can confirm that FilteringEnabled = true.

0

wait what xD. I tried the repeat thing again and it worked. thanks royaltoe.

0

update: it worked one time. going to add more info now below.

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