Hi i am trying to make a checkpoint code so when a player respawns, they spawn on their previous checkpoint but whenever they spawn in, my character falls over and i dont know why?
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)
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)
Heres the script for the checkpoint.
I feel its something to do with the Vector3 but i dont know how to sort this issue