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?
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")\
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.
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
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?
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.
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")
...
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)