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
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.