Hi, whenever i die in my obby, i get respawned to the wrong level then instantly killed until i end up spawning to my current stage. I genuinely dont know why it is doing this and ive looked over my code and not sure where to start.
Heres my code for saving the stages:
local players = game:GetService("Players")
local datastoreservice = game:GetService("DataStoreService")
local saveDataStore = datastoreservice:GetDataStore("SaveDataTest")
local function SavePlrData(plr)
local success,err = pcall(function()
local saveData = {}
for _,stat in pairs(plr.leaderstats:GetChildren()) do
saveData[stat.Name] = stat.Value
end
saveDataStore:SetAsync(plr.UserId,saveData)
end)
if not success then return err end
end
players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = stats
local data = saveDataStore:GetAsync(plr.UserId)
if data then
print(data.Stage)
for _,stat in pairs(stats:GetChildren()) do
stat.Value = data[stat.Name]
end
else
print(plr.Name.. "has no data")
end
plr.CharacterAdded:Connect(function(char)
local humanoid,hrp = char:WaitForChild("Humanoid"), char:WaitForChild("HumanoidRootPart")
wait()
if humanoid and hrp then
if stage.Value ~= 0 then
local part = workspace.Checkpoints:FindFirstChild(stage.Value)
hrp.CFrame = part.CFrame + Vector3.new(0,10,0)
local part = workspace.Checkpoints.spawn
hrp.CFrame = part.CFrame + Vector3.new(0,10,0)
end
end
end)
end)
players.PlayerRemoving:Connect(function(plr)
local err = SavePlrData(plr)
if err then print(err) end
end)
game:BindToClose(function()
for _,plr in pairs(players:GetPlayers()) do local err = SavePlrData(plr)
if err then print(err) end
end
wait(2)
end)
and heres my code for the ‘when player touches checkpoint’
local obbystages = workspace:WaitForChild("Checkpoints")
for _,stage in pairs(obbystages:GetChildren()) do
stage.Touched:Connect(function(hit)
local hum
if hit.Parent:FindFirstChild("Humanoid") then
hum = hit.Parent.Humanoid
end
if hit.Parent and hit.Parent.Parent:FindFirstChild("Humanoid") then
hum = hit.Parent.Parent.Humanoid
end
if hum then
local plr = game.Players:GetPlayerFromCharacter(hum.Parent)
local plrstage = plr.leaderstats.Stage.Value
if tonumber(stage.Name) == plrstage + 1 then
plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value + 1
elseif tonumber (stage.Name) > plrstage + 1 then
hum.Health = 0
end
end
end)
end