WaitForChild just waiting, even though child is already there

I want to use :waitforchild so I don’t get an error because the tool has not loaded yet. Sometimes, but not always, it waits forever, even though its already there


image
I tried using a repeat wait() until loop but that gave the same result.

local player=game.Players.LocalPlayer
repeat wait() until player.Team
local attack=script.Attackscript
local defend=script.Defendscript
local Update=game.ReplicatedStorage.TeamUpdater
local Pack=player.Backpack
Pack:WaitForChild("Sheild")
print("waited")
1 Like

That’s really weird. My only idea could be maybe you’re creating it on the client and checking for it on the server? Also, Shield is misspelled.

1 Like

I probably should have mentioned this but it is in StarterPack. I thought making the pack Variable might initialize it when it has 0 children, So I tried just doing Player.Backpack but that does not work either.

Now this is very strange:


I used a Task.Spawn so It will print the children while waiting for the children and the children were there but it still said it was waiting? Is this an engine issue?

task.spawn(function()
	while wait() do
		print(player.Backpack:GetChildren())
	end
end)
player.Backpack:WaitForChild("Sheild")
print("waited")
local Pack=player.Backpack

I have to imagine it is, this is so weird lol. Make a report on the devforum and try updating studio perhaps? Try replicating the issue in an empty baseplate.

Maybe try this:

...
local Pack=player.Backpack
local sheild =  Pack["Sheild"] or Pack:WaitForChild("Sheild") 
...

I did as you said and stripped the code to its bare bones.

local player=game.Players.LocalPlayer
task.spawn(function()
	while wait() do
		print(player.Backpack:GetChildren())
	end
end)
player.Backpack:WaitForChild("Shield")
print("waited")
local Pack=player.Backpack

then it started to get angry at me that backpack is not a member of player (???)
So I did this

local player=game.Players.LocalPlayer
player:WaitForChild("Backpack")
task.spawn(function()
	while wait() do
		print(player.Backpack:GetChildren())
	end
end)
player.Backpack:WaitForChild("shield")
print("waited")
local Pack=player.Backpack

And the same issue is back

This simply says that Shield is not a member of Player.Backpack
I have never tried to use a or statement for a variable so I dont know how to troubleshoot that

local player=game.Players.LocalPlayer
repeat wait() until player.Team
local attack=script.Attackscript
local defend=script.Defendscript
local Update=game.ReplicatedStorage.TeamUpdater
local Pack=player.Backpack
local sheild =  Pack["Shield"] or Pack:WaitForChild("Shield") 
print("waited")

I also fixed the name
image

to fix this you need to wait about 1 or 2 seconds before getting the player since it is running from playerscripts. not sure why but it seems to be giving you a false player/backpack. guessing this is something on roblox end on how the player is loaded in and how the localplayer variable is referenced from playerscripts

if you run same script from say starter gui it delays enough or waits on the player loaded into the .localplayer variable

so something like

task.wait(1)
local Player = game.Players.LocalPlayer
1 Like

This works fine … as StarterGui.LocalScript. In my case I was checking “Lantern”

local player=game.Players.LocalPlayer
local Pack=player.Backpack
Pack:WaitForChild("Lantern")
print("waited")

I did get that same error if I misspelled it. Is it Shield or shield.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.