Make it remember when I die

Hi all,

Im making an obby game where a message is sent to the chat whenever a player reaches a checkpoint, but I have a problem.

Whenever I die and respawn on the checkpoint, it still says that “player has reached the checkpoint”.

I want it to remember what checkpoints I’ve touched and make it not say that whenever I die.

local rs = game:GetService("ReplicatedStorage")
local chatMessage = rs:WaitForChild("ChatMessage")

local playerCheckpointData = {}

local function onCheckpointTouched(player, checkpoint)
	local character = player.Character or player.CharacterAdded:Wait()
	if not character:FindFirstChild("Humanoid") then
		character:WaitForChild("Humanoid")
	end

	if not playerCheckpointData[player] then
		playerCheckpointData[player] = {}
	end

	if playerCheckpointData[player][checkpoint.Name] then
		return
	end

	playerCheckpointData[player][checkpoint.Name] = true

	local checkpointName = checkpoint.Name
	local checkpointColor = checkpoint.BrickColor.Color
	chatMessage:FireAllClients(player, checkpointName, checkpointColor)
end

local function handleCheckpointTouch(hit, checkpoint)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then
		onCheckpointTouched(player, checkpoint)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid")
	end)

	if not playerCheckpointData[player] then
		playerCheckpointData[player] = {}
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	playerCheckpointData[player] = nil
end)

local checkpoints = workspace:WaitForChild("Checkpoints")

for _, checkpoint in pairs(checkpoints:GetChildren()) do
	if checkpoint:IsA("BasePart") then
		checkpoint.Touched:Connect(function(hit)
			handleCheckpointTouch(hit, checkpoint)
		end)
	end
end
1 Like

Well, what I’d do is:

assign an integer to each checkpoint
(E.G. Checkpoint 1, 2, 3, 4…)

Start the player off with 0 or 1 if their data is nil

When the player touches a checkpoint, it should only run code if the player’s current checkpoint is [CHECKPOINT] - 1.

In other words, it will only run if it’s the checkpoint after the previous.

If you want me to explain in detail or attempt to write some code for you let me know.

Add number value to each of the checkpoints and make call stage and then make player checkpoint or stage value be equal to the number value value and you can just do if chechpoint.stage.value <= player.leaderstats.stage.value then return end that should do it andyou add something to stop exploints and if you want to know tell me

could you please write some code. Im having trouble figuring out how to check the players current checkpoint if they have already touched the next checkpoint.

hold on, i’ll write some code in a few

you could use GetAttribute() and SetAttribute()