GetChildren not working properly

sometimes i get printed 0 sometimes 2 sometimes 1 or a strange numbes, even if i got 3 checkpoints
this problem started today also with another script eith the same issue
image

local currentLevel = game:GetService("Players").LocalPlayer.leaderstats.Stage
local totalLevels = #game.Workspace:WaitForChild("Checkpoints"):GetChildren()
local progressBar = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear)
local levelLabel = script.Parent.Parent.Parent:WaitForChild("Level")
local arrow = script.Parent.Parent.Parent:WaitForChild("Arrow")
local levelString = "Stage "
local endString = "🏆Obby completed!🏆"
print(totalLevels)
progressBar.Size = UDim2.fromScale(1/totalLevels, 1)
arrow.Position = UDim2.fromScale(1/totalLevels, .8)

levelLabel.Text = levelString..currentLevel.Value.." ("..math.round(currentLevel.Value/totalLevels*100).."%)"

currentLevel.Changed:Connect(function()
	
	levelLabel.Text = levelString..currentLevel.Value.." ("..math.round(currentLevel.Value/totalLevels*100).."%)"
	
	local percent = currentLevel.Value/totalLevels
	
	if	percent >= 0.05 then
		
		tweenService:Create(progressBar, tweenInfo, {Size = UDim2.fromScale(percent, 1)}):Play()
		tweenService:Create(arrow, tweenInfo, {Position = UDim2.fromScale(percent, .8)}):Play()
	
	end
	
	if currentLevel.Value == totalLevels then
		
		wait(tweenInfo.Time)
		
		arrow:Destroy()
		
		wait(.5)
		
		levelLabel.Text = endString
		
	end
	
end)

how do i get the amount of checkpoints there are in the game by removing it?

I misread the problem, the problem is that the checkpoints load some time after you call :GetChildren() but unfortunately there’s no way of waiting for all the children of an object to load, you should add a simple yield or just calculate the amount of checkpoints when you need it.

it started working back with no changes on the game it doesnt seem the issue is the script

Well you shouldn’t initialize values like that on the client, I’d still highly recommend adding a small yield (something like task.wait(3)) at the beginning of the script.

thanks i will try this, i hope to find a better way becose the wait time depends on the local device

I am currently updating the count manually

ANY FIX IDEAS ACCEPTED

Is that a local script? if yes, you should at least wait until game method IsLoaded returned true before getting anything.

1 Like

to add on to this, localscripts have a tendency to not detect objects in workspace even though they are clearly there.

Count.rbxm (3,5 КБ)

image

  1. ServerScriptService
  2. ReplicatedStorage
  3. The location where the script from your screenshot is located

Perhaps use ChildAdded or ChildRemoved to update the level count?

I am making the count server sided

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.