local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local PlayersGameProfile = DataStoreService:GetDataStore("GameProfile")
local GameStagesFolder = workspace.stage_checkpoint
local function SaveData(Player: Player)
-- adding this later
end
local function CheckPointTP(Character: Model)
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
local LeaderStats = Player.leaderstats
local Stags: IntValue = LeaderStats.Stage
for _, CheckPoint: BasePart in pairs(GameStagesFolder:GetChildren()) do
if CheckPoint:GetAttribute("CheckIndex") == Stags.Value then
Character:PivotTo(CheckPoint.CFrame + Vector3.new(0, 3, 0))
print("sss")
return
end
end
end
end
local function LoadData(Player: Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local DataFolder, Stage, Death = Instance.new("Folder"), Instance.new("IntValue"), Instance.new("IntValue")
DataFolder.Parent = Player; Stage.Parent = DataFolder; Death.Parent = DataFolder
DataFolder.Name = "leaderstats"; Stage.Name = "Stage"; Death.Name = "Deaths"
local PlayerData = PlayersGameProfile:GetAsync(tostring(Player.UserId) .. "-MO")
if PlayerData then
Stage.Value = if PlayerData["Stages"] then PlayerData["Stages"] else 1
Death.Value = if PlayerData["Deaths"] then PlayerData["Deaths"] else 0
else
Stage.Value = 2
Death.Value = 0
end
CheckPointTP(Character)
print("from load")
end
Players.PlayerAdded:Connect(function(Player: Player)
LoadData(Player)
Player.CharacterAdded:Connect(function(char)
CheckPointTP(char)
print("from added")
end)
end)
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, player in pairs(Players:GetChildren()) do
SaveData(player)
end
end)
Have you tried printing the CheckPoint name when the player respawns and see what it prints out? (To see if it prints the correct value, although from what I see from the code provided nothing seems wrong on that part)
Is there a possibility that something is overriding the position of the player after your CheckPointTP function?
By any chance, do you have a default Roblox spawn set at the 1st checkpoint position?
Edit 1 : Based on what’s given, I think its very likely that something else is overriding the TP function you provided as I don’t see any problems within your code (unless I’ve overlooked any).
Edit 2 : Perhaps try adding a task.wait before letting the CheckPointTP function run?
i have check to see if any scripts that can overwrite it however there were none, i also printed the stage and checkpoint and they are correct so far.
Yes the default roblox spawn position is at 1st checkpoint so incase the checkpoint didnt teleport the player or else they will die many times in to the void
i just added task.wait() before the PivotTo function is called it works!
local function CheckPointTP(Character: Model)
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
local LeaderStats = Player.leaderstats
local Stags: IntValue = LeaderStats.Stage
for _, CheckPoint: BasePart in pairs(GameStagesFolder:GetChildren()) do
if CheckPoint:GetAttribute("CheckIndex") == Stags.Value then
task.wait()
Character:PivotTo(CheckPoint.CFrame + Vector3.new(0, 3, 0))
return
end
end
end
end