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

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

Asked by
Gunt_r 31
4 years ago

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

Log in to vote
1
Answered by
Gunt_r 31
4 years ago
Edited 4 years ago

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()
Ad
Log in to vote
0
Answered by
TomsGames 225 Moderation Voter
4 years ago
Edited by royaltoe 4 years ago

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.

0
repeat wait() until player.Character Works. local character = player.Character or player.CharacterAdded:Wait() is even better royaltoe 5144 — 4y
0
"If your game does not have FilteringEnabled" FE is forced on all games DeceptiveCaster 3761 — 4y
0
It can be turned off though, right? So if he had turned it off... TomsGames 225 — 4y
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.  royaltoe 5144 — 4y
View all comments (4 more)
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. Gunt_r 31 — 4y
0
I also havent meddled with filtering enabled. I just checked it again and can confirm that FilteringEnabled = true. Gunt_r 31 — 4y
0
wait what xD. I tried the repeat thing again and it worked. thanks royaltoe. Gunt_r 31 — 4y
0
update: it worked one time. going to add more info now below. Gunt_r 31 — 4y

Answer this question