Hi there, a Mom here trying to make a game with my boys. lol
trying to make an Obby. One that when you leave, it saves your current position from when you last played, and the stage you were on.
This is the script that saves my last position I placed in "Server Script Service"
local ds = game:GetService("DataStoreService")
local dsl = ds:GetDataStore("positionsave")
game.Players.PlayerAdded:Connect(function(p)
local pos = Instance.new("Vector3Value",p)
pos.Name = "Position"
local userdata
pcall(function()
userdata = dsl:GetAsync(p.UserId)
end)
if userdata then
game.Workspace:WaitForChild(p.Name):WaitForChild("HumanoidRootPart").CFrame = CFrame.new(userdata[1], userdata[2],userdata[3])
else
dsl:SetAsync(p.UserId,{ 0, 3, 0})
end
end)
game.Players.PlayerRemoving:Connect(function(p)
dsl:SetAsync(p.UserId, {
math.floor(p.Position.Value.X),
math.floor(p.Position.Value.Y),
math.floor(p.Position.Value.Z)
})
end)
game.Players.PlayerAdded:Connect(function(p)
local pos = p:WaitForChild("Position")
p.CharacterAdded:Connect(function(char)
while true do
pos.Value = char:WaitForChild("HumanoidRootPart").Position
wait(1)
end
end)
end)
So YES it works on saving my position, but SAY I was on Stage 2 and leave the game, When rejoining it loaded my character onto Stage 2, but it said Stage 0 in my leaderboard,
I need help with a script, for my obby, that saves my players position and the stage they were last on.
Here is my Checkpoint Script that is placed in my "Workspace"
local STAT_NAME = "Stage"
local PREVENT_SKIPPING = true
local checkpoints = {}
local i = 1
while true do
local checkpoint = Workspace:FindFirstChild("Checkpoint " .. i, true)
if not checkpoint then print("Last Checkpoint : " .. i-1) break end
table.insert(checkpoints, checkpoint)
i = i + 1
end
game.Players.PlayerAdded:connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats") or Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = STAT_NAME
checkpointStat.Value = 0
player.CharacterAdded:connect(function(character)
local goto = checkpoints[checkpointStat.Value]
if goto then
repeat wait() until character.Parent
character:MoveTo(goto.Position)
else
warn("Checkpoint " .. checkpointStat.Value .. " not found")
end
end)
end)
for index, checkpoint in ipairs(checkpoints) do
checkpoint.Touched:connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid or humanoid.Health <= 0 then return end
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
local checkpointStat = leaderstats:FindFirstChild(STAT_NAME)
if not leaderstats then return end
if (PREVENT_SKIPPING and checkpointStat.Value + 1 == index) or (not PREVENT_SKIPPING and checkpointStat.Value < index) then
checkpointStat.Value = index
end
end)
end

1 comment
Please use codeblocks.