[UNSOLVED] Infinite yield possible on Leaderstats:WaitForChild

There was always 1 working stat but if I copy that exact code and change it with its needs it still doesn’t work, i’ve even tried what would happen if I completely copied over the script from the working one but then none work, does anybody know why this may be happening, this does only occur on public servers, not on local servers and when there are more than 2 players in a public server.


4 Likes

Try putting

repeat
    wait()
until
Player.leaderstats:FindFirstChild("stat name here")

before using WaitForChild

1 Like

I was using FindFirstChild before but that had the same problem

1 Like

What is the script that sets them up? That might be to do with it.

Do the stats actually load? WaitForChild throws that warning after 5 seconds, it might take longer than that for the stat to load.

It may indeed take longer, all the stats and everything work fine if its a new server but when a second player joins it wont work for them as it should, should I add a wait?

Can you show the code? That’ll help figuring out the problem.

Did you add the proper children into leaderstats?

This one always works:

local TextLabel = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Module = require(game.StarterGui.Module:WaitForChild("UnLockedModule"))

local Abbrevations_ = { "K", "M", "B", "T", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ" }

local function toHumanReadableNumber(num)
	if Module.Data.Abbrevations == 1 then
		if num < 1000 then
			return tostring(num)
		end
		local digits = math.floor(math.log10(num)) + 1
		local index = math.min(#Abbrevations_, math.floor((digits - 1) / 3))
		local front = num / 10^(index * 3)
		return string.format("%.1f%s+", front, Abbrevations_[index])
	elseif Module.Data.Abbrevations == 0 then
		local formattedNumber = tostring(num)
		local parts = {}
		local length = #formattedNumber
		local remainder = length % 3
		if remainder ~= 0 then
			parts[#parts + 1] = formattedNumber:sub(1, remainder)
		end
		for i = remainder + 1, length, 3 do
			parts[#parts + 1] = formattedNumber:sub(i, i + 2)
		end
		return table.concat(parts, ".")
	else
		return tostring(num)
	end
end

function updateTextLabel(value)
	TextLabel.Text = toHumanReadableNumber(value)
end

local function refresh()
	local towerTokensValue = Player:WaitForChild("leaderstats"):WaitForChild("TowerTokens").Value
	updateTextLabel(towerTokensValue)
end

local function checkForAbbrevationsChange()
	local currentValue = Module.Data.Abbrevations
	while true do
		wait(1)
		if Module.Data.Abbrevations ~= currentValue then
			refresh()
			currentValue = Module.Data.Abbrevations
		end
	end
end

Player:WaitForChild("leaderstats"):WaitForChild("TowerTokens").Changed:Connect(refresh)
spawn(checkForAbbrevationsChange)

refresh()

and this one uses a wait but doesnt work for the second player in the server:

local TextLabel = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Module = require(game.StarterGui.Module:WaitForChild("UnLockedModule"))

local Abbrevations_ = { "K", "M", "B", "T", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ" }

local function toHumanReadableNumber(num)
	if Module.Data.Abbrevations == 1 then
		if num < 1000 then
			return tostring(num)
		end

		local digits = math.floor(math.log10(num)) + 1
		local index = math.min(#Abbrevations_, math.floor((digits - 1) / 3))
		local front = num / 10^(index * 3)

		return string.format("%.1f%s+", front, Abbrevations_[index])
	elseif Module.Data.Abbrevations == 0 then
		local formattedNumber = tostring(num)
		local parts = {}
		local length = #formattedNumber
		local remainder = length % 3
		if remainder ~= 0 then
			parts[#parts + 1] = formattedNumber:sub(1, remainder)
		end
		for i = remainder + 1, length, 3 do
			parts[#parts + 1] = formattedNumber:sub(i, i + 2)
		end
		return table.concat(parts, ".")
	else
		local formattedNumber = tostring(num)
		local parts = {}
		local length = #formattedNumber
		local remainder = length % 3
		if remainder ~= 0 then
			parts[#parts + 1] = formattedNumber:sub(1, remainder)
		end
		for i = remainder + 1, length, 3 do
			parts[#parts + 1] = formattedNumber:sub(i, i + 2)
		end
		return table.concat(parts, ".")
	end
end

function updateTextLabel(value)
	TextLabel.Text = toHumanReadableNumber(value)
end

local towerTokensValue = Player:WaitForChild("leaderstats"):WaitForChild("Wins").Value
updateTextLabel(towerTokensValue)

if game.ReplicatedStorage:WaitForChild("DataStoreLoadedV").Value == 0 then
	game.ReplicatedStorage:WaitForChild("DataStoreLoadedV"):GetPropertyChangedSignal("Value")
	wait(5)
else
	wait(5)
end

local function refresh()
	local towerTokensValue = Player:WaitForChild("leaderstats"):WaitForChild("Wins").Value
	updateTextLabel(towerTokensValue)
end

local function checkForAbbrevationsChange()
	local currentValue = Module.Data.Abbrevations
	while true do
		wait(1)
		if Module.Data.Abbrevations ~= currentValue then
			refresh()
			currentValue = Module.Data.Abbrevations
		end
	end
end

Player:WaitForChild("leaderstats"):WaitForChild("Wins").Changed:Connect(refresh)
spawn(checkForAbbrevationsChange)

refresh()

I think it’s trying to wait for a child that doesn’t exist have you checked for spelling mistakes?

Yes, I already have and i’ve spend days already trying to fix it but it just wont work, the same with other stuff, they work in new baseplates, other places under that game on behalf of that place, for me the lobby, there are many, kinda weird tbh, the code works, no spelling mistakes and still wont work for the second player, it does for the first player that joins a new server.

Hmm, that’s all I could think of since I’m a bit new to this. Sorry if I couldn’t help. Good luck!

No problem, thank a lot though for your time.

1 Like

simple

leaderstats:WaitForChild("CHILDNAME",30)

By adding the 30, it’ll wait 30 seconds or until the child loads in, this will solve the error


Still the same problem, the last errors are on 2 out of 4 scripts I added the wait to. Those are new.

This is my current code, this is the second to last error you can see:

local TextLabel = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Module = require(game.StarterGui.Module:WaitForChild("UnLockedModule"))

local Abbrevations_ = { "K", "M", "B", "T", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ" }

local function toHumanReadableNumber(num)
	if Module.Data.Abbrevations == 0 then
		if num < 1000 then
			return tostring(num)
		end

		local digits = math.floor(math.log10(num)) + 1
		local index = math.min(#Abbrevations_, math.floor((digits - 1) / 3))
		local front = num / 10^(index * 3)

		return string.format("%.1f%s+", front, Abbrevations_[index])
	elseif Module.Data.Abbrevations == 1 then
		local formattedNumber = tostring(num)
		local parts = {}
		local length = #formattedNumber
		local remainder = length % 3
		if remainder ~= 0 then
			parts[#parts + 1] = formattedNumber:sub(1, remainder)
		end
		for i = remainder + 1, length, 3 do
			parts[#parts + 1] = formattedNumber:sub(i, i + 2)
		end
		return table.concat(parts, ".")
	else
		local formattedNumber = tostring(num)
		local parts = {}
		local length = #formattedNumber
		local remainder = length % 3
		if remainder ~= 0 then
			parts[#parts + 1] = formattedNumber:sub(1, remainder)
		end
		for i = remainder + 1, length, 3 do
			parts[#parts + 1] = formattedNumber:sub(i, i + 2)
		end
		return table.concat(parts, ".")
	end
end

function updateTextLabel(value)
	TextLabel.Text = toHumanReadableNumber(value)
end

local towerTokensValue = Player:WaitForChild("leaderstats"):WaitForChild("Wins",30).Value
updateTextLabel(towerTokensValue)

if game.ReplicatedStorage:WaitForChild("DataStoreLoadedV").Value == 0 then
	game.ReplicatedStorage:WaitForChild("DataStoreLoadedV"):GetPropertyChangedSignal("Value")
	wait(5)
else
	wait(5)
end

local function refresh()
	local towerTokensValue = Player:WaitForChild("leaderstats"):WaitForChild("Wins",30).Value
	updateTextLabel(towerTokensValue)
end

local function checkForAbbrevationsChange()
	local currentValue = Module.Data.Abbrevations
	while true do
		wait(1)
		if Module.Data.Abbrevations ~= currentValue then
			refresh()
			currentValue = Module.Data.Abbrevations
		end
	end
end

Player:WaitForChild("leaderstats"):WaitForChild("Wins",30).Changed:Connect(refresh)
spawn(checkForAbbrevationsChange)

refresh()

Can I see your workspace for leaderstats?

image
here is my Workspace, btw my leaderstats are in ServerScriptService.

Ok wait is leaderstats a folder? if so whats in it?

Its a folder thats stored in the player, it stores values in game.Players.[Player].leaderstats.[Stat] accessible with game.Players.[Player].leaderstats.[Stat].Value
image

Wait which errors are coming from this script?

From the script that displays the value or from leaderstats because leaderstats doesn’t give errors rn?