How to detect children being added to the folder

Hello guys, so I created an obby where it’s generated randomly for each server but the thing is I want the user’s checkpoint to save using this script so whenever they leave the game and join a different server you’ll be teleported back to your current level.

Unfortunately the problem with this script is that it doesn’t work on new servers because there’s no children on “checkpoints” since the new servers will have to generate the checkpoints first and then add it to the folder. The script doesn’t detect the new checkpoints being added to the folder.

How do I make it so that the script will also detect the new checkpoints being added to the folder? and how do I also add so that when the user datastore is currently at a higher level but the checkpoints haven’t loaded there yet, the game will make you wait for the checkpoints to be completely added to the game via a loading screen before you spawn in.

I’m not an experience programmer myself and I frequently struggle with the basics sometimes, I would highly appreciate any response or help. Thank you!

Here’s the full script for details:

----- Variables -----

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local ObbyDataStore = DataStoreService:GetDataStore("DataCheckpointTester") -- Test Data Store, Not official Data

local checkpoints = game.Workspace.Checkpoints -- Checkpoint Folder-

local FIRST_CHECKPOINT = 1

----- Events -----

Players.PlayerAdded:Connect(function(player)
	local playerKey = "ID_" .. player.UserId
	local retrievedData
	
	local success, errorMessage = pcall(function()
		retrievedData = ObbyDataStore:GetAsync(playerKey)
	end)
	
	if not success and not RunService:IsStudio() then
		player:Kick("Error loading data, please rejoin.")
		return
	end
	
	if not retrievedData then
		retrievedData = {}
	end
	
	-- Create leaderstats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- Create stage value
	local stageValue = Instance.new("IntValue")
	stageValue.Name = "Stage"
	stageValue.Value = retrievedData.Stage or FIRST_CHECKPOINT
	stageValue.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		wait() -- Wait for the character to load
		
		local checkpoint = checkpoints:FindFirstChild(tostring(stageValue.Value))
		
		if checkpoint then
			character:WaitForChild("HumanoidRootPart").CFrame = checkpoint.CFrame + Vector3.new(0, 3 + (checkpoint.Size.Y / 2), 0)
		end
	end)
end)

for i, v in pairs(checkpoints:GetChildren()) do
	v.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		
		if not player or not player:FindFirstChild("leaderstats") then
			return
		end
		
		if player.leaderstats.Stage.Value == tonumber(v.Name) - 1 then -- Prevents the player from skipping checkpoints and going backward
			player.leaderstats.Stage.Value = tonumber(v.Name)
		end
	end)
end

game.Players.PlayerRemoving:Connect(function(player)
	if not player:FindFirstChild("leaderstats") then
		return
	end
	
	local playerKey = "ID_" .. player.UserId
	local stage = player.leaderstats.Stage.Value
	
	local success, errorMessage = pcall(function()
		ObbyDataStore:SetAsync(playerKey, {Stage = stage})
	end)
	
	if not success and not RunService:IsStudio() then
		warn("Error while saving player data")
	end
end) 
1 Like

If you are wondering how to detect new children of a object here’s how:

local object = someobjecthere --the object you want to detect children of

object.ChildAdded:Connect(function(child)
   --some stuff here with child
end)

And for waiting you could try using loops like while and some bool variable which
will tell when checkpoints are loaded that will be checked every loop
OR
you could try ChildAdded function make some variable become true once players checkpoint will load and meanwhile in while loop it will just wait for it and then I guess load the player in. That’s my guess.

4 Likes

Thank you for the response, I didn’t thought of using the ChildAdded function my bad. :sweat_smile:

It’s alright, reply if it will work, it will be very good to hear that you succeeded.

To listen for children being added to a parent you can do:

local BaseParent = path.to.instance

BaseParent.ChildAdded:Connect(function(NewChild)

end)

NewChild is the child that got added to the parent.

2 Likes