So i wrote a pretty sensible code to make a checkpoint work when it is stepped on. How do i make it so when you die, it will teleport you to the checkpoint? Here is my attempt:
local checkFolder = game.Workspace.checkpoints game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = plr local checkpoints = Instance.new("IntValue") checkpoints.Name = "Stages" checkpoints.Parent = leaderstats for i,v in pairs(checkFolder:GetChildren()) do if v:IsA("Part") then v.Touched:Connect(function(plr) if i == 1 then checkpoints.Value = 1 if plr.Parent:WaitForChild("Humanoid").Health == 0 then plr.Parent:WaitForChild("Humanoid").Position = checkFolder.Check1.Position end end) end end end end)
Your code is almost right. My way to do this would be to create a dictionary of players' spawn positions, then update those when they touch something. Then for every who joins I'd connect to their characteradded and move them there. Example below:
local folder = game.Workspace.Folder local playerpositions = {} local spawnpoint = game.Workspace.SpawnPoint for i, v in next, folder:GetChildren() do if v:IsA("BasePart") then v.Touched:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr == nil then return end playerpositions[plr] = v.Position end) end end game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = plr local checkpoints = Instance.new("IntValue") checkpoints.Name = "Stages" checkpoints.Parent = leaderstats plr.CharacterAdded:Connect(function(char) char:MoveTo(playerpositions[plr] or spawnpoint.Position) end) end)
Reply with further questions,
and please mark as correct if this solution works for you!