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