Script not finding child while it finds other without issues

So i have a folder under the player named “stats” and i have a string value under that folder named “server1Size” all places where i try to get it, it says “server1Size is not a valid memeber of folder” but it is and i also have a string value under stats named “server1power” and there’s no issues with this one.
The problem is not that i don’t wait for it to exist because when i use “WaitForChild” it says “Infinite possible yield on WaitForChild(“server1Size”)” can anyone explain?
Here’s my code

   -- Server Script

    -- Folders --
    local stats = Instance.new("Folder", player)
    stats.Name = "stats"

    -- Values --
	local money = Instance.new("StringValue", stats)
	money.Name = "money"
	money.Value = "0"
	
	local wood = Instance.new("StringValue", stats)
	wood.Name = "wood"
	wood.Value = "0"
	
	local server1 = Instance.new("BoolValue", stats)
	server1.Name = "server1"
	server1.Value = true
	
	local server1Size = Instance.new("StringValue", stats)
	server1Size.Name = "server1Size"
	server1Size.Value = "0"
    -- This is the one with the problem
	
	local server1Power = Instance.new("StringValue", stats)
	server1Power.Name = "server1Power"
	server1Power.Value = "0"
	
	local server1SizePrice = Instance.new("StringValue", stats)
	server1Size.Name = "server1SizePrice"
	server1Size.Value = "100"

	local server1PowerPrice = Instance.new("StringValue", stats)
	server1Power.Name = "server1PowerPrice"
	server1Power.Value = "100"

    -- Other script where there's error's

   game.ReplicatedStorage.Remote.Server.GetServerInfo.OnServerInvoke = function(player)
      local plr = game.Players:FindFirstChild(player.Name)
      local stats = plr.stats
      stats:WaitForChild("server1Size") -- Error: Infinite possible yield
      local data = {}
      data[1] = stats.server1.Value -- No errors
      data[2] = stats.server1Size.Value --- Error: server1Size not a valid member of folder
      return data
   end
2 Likes

How are you getting the player? Are you using a PlayerAdded event?

In the first script yes. In the other i am getting the players name from remoteFunction

Can I see the whole script? Its hard to figure out the problem with a tiny bit of the script.

First script

local datastore = game:GetService("DataStoreService")

local key = 1

local MainStore = datastore:GetDataStore("Main")

local failed = {}

game.Players.PlayerAdded:Connect(function(player)
	-- Folders --
	local stats = Instance.new("Folder", player)
	stats.Name = "stats"

	-- Values --
	local money = Instance.new("StringValue", stats)
	money.Name = "money"
	money.Value = "0"
	
	local wood = Instance.new("StringValue", stats)
	wood.Name = "wood"
	wood.Value = "0"
	
	local server1 = Instance.new("BoolValue", stats)
	server1.Name = "server1"
	server1.Value = true
	
	local server1Size = Instance.new("StringValue", stats)
	server1Size.Name = "server1Size"
	server1Size.Value = "0"
	
	local server1Power = Instance.new("StringValue", stats)
	server1Power.Name = "server1Power"
	server1Power.Value = "0"
	
	local server1SizePrice = Instance.new("StringValue", stats)
	server1Size.Name = "server1SizePrice"
	server1Size.Value = "100"

	local server1PowerPrice = Instance.new("StringValue", stats)
	server1Power.Name = "server1PowerPrice"
	server1Power.Value = "100"

	-- dataStore	
	local dataMain
	local succes, errorMessage = pcall(function()
		dataMain = MainStore:GetAsync(player.UserId.."Main"..key)
	end)

	-- setValuesFromDataStore	
	if dataMain then
		if #dataMain == 7 then
			money.Value = dataMain[1]
			server1.Value = dataMain[2]
			server1Size.Value = dataMain[3]
			server1Power.Value = dataMain[4]
			server1SizePrice.Value = dataMain[5]
			server1PowerPrice.Value = dataMain[6]
			wood.Value = dataMain[7]
		end
	elseif not succes then
		failed[player.Name] = player.Name
		player:Kick("Your data was not recieved from server please rejoin")
	end
	game.ReplicatedStorage.Remote.Client.Joined:FireClient(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	if not (failed[player.Name]) then
		local main = {}
		player.stats:WaitForChild("server1Size")
		main[1] = player.stats.money.Value
		main[2] = player.stats.server1.Value
		main[3] = player.stats.server1Size.Value
		main[4] = player.stats.server1Power.Value
		main[5] = player.stats.server1SizePrice.Value
		main[6] = player.stats.server1PowerPrice.Value
		main[7] = player.stats.wood.Value

		pcall(function()
			MainStore:SetAsync(player.UserId.."Main"..key, main)
		end)
	else
		failed[player.Name] = nil
	end
end)

Second script

game.ReplicatedStorage.Remote.Server.GetServerInfo.OnServerInvoke = function(player)

local plr = game.Players:FindFirstChild(player.Name)

local stats = plr.stats

stats:WaitForChild("server1Size")

local data = {}

data[1] = stats.server1.Value

data[2] = stats.server1Size.Value

return data

end

game.ReplicatedStorage.Remote.Server.GetStatsUpgrade.OnServerInvoke = function(player)

local plr = game.Players:FindFirstChild(player.Name)

local stats = plr.stats

local data = {}

stats:WaitForChild("server1Size")

data[1] = stats.money.Value

data[2] = stats.wood.Value

data[3] = stats.server1SizePrice

data[4] = tostring(math.pow(tonumber(stats.server1Size), 10))

data[5] = stats.server1PowerPrice

data[6] = tostring(math.pow(tonumber(stats.server1Power), 2))

end

your problem is that where you are putting all your values into the stat folder under server1SizePrice u put server1Size instead of server1SizePrice

local server1SizePrice = Instance.new("StringValue", stats)
	server1Size.Name = "server1SizePrice" -- replace the server1Size to server1SizePrice
	server1Size.Value = "100" -- replace the server1Size to server1SizePrice

should fix it

Your looking the wrong place i have a thing called server1Size and another server1SizePrice

no but when i used the script and if you play test and then check the folder there is no server1Size because you are using server1Size in the the server1SizePrice part so you do have server1Size just you renamed it to server1SizePrice just try it thats why you cant find it using WaitForChild since there is no server1Size

i just looked again and your right. I feel stupid now lol

1 Like