Finding an instance through a string

Hey! I’m trying to do a currency system with GUI but I couldn’t find a solution about getting the value which is in a folder that is named as Player’s ID.

local RS = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlayerID = Player.UserId
local PlayerdataFolder = RS.PlayerData
local PlayerdataFolder2 = RS.PlayerID
local Cash = PlayerdataFolder2.Coins

while true do
	wait(0.04)
	script.Parent.Text = Cash.value
end
local PlayerdataFolder2 = RS[tostring(PlayerID)]
-- or 
local PlayerdataFolder2 = RS:FindFirstChild(PlayerID)
1 Like

You would use square brackets, like this ["string_here"] (or as @MakerDoe suggested, :FindFirstChild()).
Your variable, would become:

local PlayerdataFolder2 = RS[tostring(PlayerID)]

You may also use :WaitForChild to make sure the instance is actually there when looking for it.

local PlayerdataFolder2 = RS:WaitForChild(tostring(PlayerID))
1 Like