Local script can't locate the players' backpack

I am trying to get a StringValue called “CharacterChosen” located inside of the local player’s backpack. Here is the script I used:

local player = game.Players.LocalPlayer
local characterChosen = game.Players[player.Name].Backpack.CharacterChosen

When I ran the game, it returned an error saying that the Backpack is not a member of RichPerson03return (My username)

I thought it was because the Backpack didn’t load yet, so I used WaitForChild

local player = game.Players.LocalPlayer
local characterChosen = game.Players[player.Name]:WaitForChild("Backpack").CharacterChosen

Then it was successfully able to locate the backpack, but then it said CharacterChosen was not a member of Backpack.

So I used WaitForChild again…

local player = game.Players.LocalPlayer
local backpack = game.Players[player.Name]:WaitForChild("Backpack")
local characterChosen = backpack:WaitForChild("CharacterChosen")

Then it said there was an infinite yield possible on ‘Players.RichPerson03return.Backpack:WaitForChild(“CharacterChosen”)’

I don’t know what infinite yield means. Can someone please tell me what I can do for this?

2 Likes
2 Likes

I recommend tidying up a bit you already retrived Player you should use variables you have already made

You do this…

local player = game.Players.LocalPlayer
local backpack = game.Players[player.Name]:WaitForChild("Backpack")
local characterChosen = backpack:WaitForChild("CharacterChosen")

Using the Player variable you can do this

local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local characterChosen = backpack:WaitForChild("CharacterChosen")

What I would have done

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local CharacterChosen = Backpack:WaitForChild("CharacterChosen")\

I did that, and it did the exact same thing. It didn’t locate the StringValue and it said infinite yield possible.

Can you play test and look for this CharacterChosen String Value within Explorer tab

Open Players > Backpack > and find CharacterChosen

Yes, that’s what I did on the client and I can see it. I can even see it on the server. The only information it gives me is infinite yield possible.

I also checked if it actually did get the StingValue by putting this code:

print(characterChosen.Value)

and it printed stack begin and stack end with some code and stuff.

I am very confused can you send it here

oh sorry I lied, it didn’t print out anything actually. I was looking at this:

It returns me to the line where I tried to define the StringValue. I used the print line after it, so that’s why it didn’t print, because it’s infinitely searching for the StringValue I think.

bruhhhhhh

1 Like

Send an image of the client’s explorer with Backpack open. It’s probably just a typo, they need to exactly match their names.

I took the image while the game was running on my client.

backpack

Also, I don’t know if I need to say this, but the LocalScript I’m using to locate the StringValue is in StarterPlayerScripts

1 Like

Its detecting the backpack just not the CharacterChosen value inside

Whats creating the CharacterChosen value and parenting it to the backpack?

Nothing, actually. I just put the StringValue in StarterPack

Well WaitForChild() doesn’t yield forever until it finds the target, It yields for 5 seconds until it throws a warning

I’m guessing manually?

Is the WaitForChild warning appearing before you’re able to move around? It waits a default of 5 seconds; I just want to make sure you don’t need more time.

Another possibility - is there any spaces or other characters appended to the back of the value name?

You could try testing out if your script could find it:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
wait(10)
for _, child in ipairs(Backpack:GetChildren()) do
  print(child.Name)
end

See if that prints out ‘CharacterChosen’

The warning appears after 5 seconds when I test.

Also I put in the code you did, and after waiting 10-20 seconds it didn’t print anything. That must mean it doesn’t recognize any children of the backpack, right?

1 Like

If you put the StringValue while the game is running and you’re on server mode, the local script won’t be able to detect it because it’s not on the client.

So I’ve done some testing in studio and managed to replicate the bug. Oddly enough, it’s some kind of bug with backpack…

The following code does NOT work:

local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local characterChosen = backpack:WaitForChild("CharacterChosen")

The follow code DID work:

local player = game.Players.LocalPlayer
wait(3)
local backpack = player.Backpack
local characterChosen = backpack:WaitForChild("CharacterChosen")

It’s very strange… I don’t understand why the backpack WaitForChild() is returning isn’t the same. I am still looking into it.

Related: Backpack.ChildAdded fires; child added is not added though

Edit -
After reading that post, it sounds like the first Backpack that is put into the player gets replaced by another Backpack. You are safe to index Backpack as long as the character exists though:

local player = game.Players.LocalPlayer
_ = player.Character or player.CharacterAdded:Wait() 
local backpack = player.Backpack
local characterChosen = backpack:WaitForChild("CharacterChosen")
...
3 Likes

It worked after waiting 3 seconds before defining the backpack. Thanks! I’ll reply if I find anymore issues, but I probably won’t.

Update: I figured out that it also works if you wait 1 second, so when the player is done loading they didn’t even realize it had to wait. (If you used a visualizer)

1 Like