Help me understand a part of a script

Hello there,

  1. What do I want to achieve? Make another part be the one two make the leaderstats go up by 1

  2. What is the issue? The issue is that i do not know how which part of the script makes the leaderstats go up by one

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 = "Level"
	stageValue.Value = retrievedData.Level 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.Level.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. What solutions have you tried so far? I tried looking up some vids, but they didn’t explain how the script works or the script just does not work.

this script loads a datastore for each player and makes a leaderstats folder with a stage int value
it then loops trough each part in the checkpoints instance (that you are missing!) and whenever a player touches it, checks if the player’s level is the last one before the checkpoint (based off of its numerical name) and changes their level

you have to fix this part to be
if player.leaderstats.Stage.Value == tonumber(v.Name) - 1 then
and add this to the start add

local checkpoints = workspace:WaitForChild("checkpoints")
local FIRST_CHECKPOINT = 0

then make a folder named “checkpoints” in the workspace and add parts in it that are named in order. So you should have:

a folder named “checkpoints”
a part inside the folder named 1
a part inside the folder named 2
and so on.

Thank you, for explaining the code and fixing my errors, but which part of the script makes it that if we touch another checkpoint, we go to Stage 2?

Thats all automatic in this part:

whenever a checkpoint is touched by a player It reads the checkpoint’s name and checks if the player was in the last one. If they were, set them to the new one

1 Like

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