i have a game which is an obby. i have a seconds script and a deaths script which work and they save in a datastore when someone leaves. I am trying to add in a checkpoint script but I do not know how. i have tried for a week but i do not know. Could you help?
Just have a Number Value in leader stats.
Than have scripts update it to what stage your on, through a touched event.
how do i make it so they spawn there when they die and join. could you show me a script plz
We do not want to spoonfeed you, you have to do your script yourself.
As @Wizard101fire90 said, you can add a NumberValue and get it’s value that corresponds to the Checkpoint number. Use CharacterAdded if you want the player to spawn to the Checkpoint when they reset or died.
how should i make it so they spawn there when they spawn die and join
You can use the CharacterAdded
event. Link about it: Player | Documentation - Roblox Creator Hub
For the stage leaderstats, I’d do an IntValue, because an Int can only be whole numbers 0 or above (no decimals). Also, using .CharacterAdded
can be used to make sure the player spawns at the right checkpoint when they die.
game.Players.PlayerAdded:Connect(function(player)
local ls = Instance.new("Folder", player)
ls.Name = "leaderstats"
local stage = Instance.new("IntValue", ls)
stage.Name = "Stage"
stage.Value = 0
player.CharacterAdded:Connect(function(char)
local humpart = char.HumanoidRootPart
humpart.CFrame = workspace.Checkpoints:FindFirstChild(stage.Value).CFrame
end)
end)
Next, You would make a folder named “Checkpoints”, and inside of it would be all of your checkpoints. For each checkpoint, you would name it by its number (ex: Checkpoint 1 = “1”, checkpoint 21 = “21”) Along with that, you would put a server script in the Checkpoints folder, and type in it:
for _, v in pairs(script.Parent:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit.Parent.Humanoid then
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
if player.leaderstats.Stage.Value == tonumber(v.Name) - 1 then
player.leaderstats.Stage.Value = tonumber(v.Name)
end
end
end)
end
end
That script above will work for all the checkpoints inside the folder, so you won’t need 100s of scripts.
Hope this helps
I am getting this error:
Change the checkpoint script to this:
for _, v in pairs(script.Parent:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
local char = hit:FindFirstAncestorOfClass("Model")
local player = game.Players:GetPlayerFromCharacter(char)
if player.leaderstats.Stage.Value == tonumber(v.Name) - 1 then
player.leaderstats.Stage.Value = tonumber(v.Name)
end
end)
end
end
I am getting this error which is stopping my game from saving. Also I added the humanoidrootpart part to humanoid died function:
The script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local ga = false
Players.PlayerAdded:Connect(function(Player)
local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
warn("Initializing leaderstats...")
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = Player
local wipeouts = Instance.new("IntValue")
wipeouts.Name = "Deaths"
wipeouts.Parent = stats
local second = Instance.new("IntValue")
second.Name = "Seconds"
second.Parent = stats
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = stats
print("Completed.")
warn("Initializing leaderstats values...")
local success = PlayerDataStore:GetAsync("success")
local PlayerData = PlayerDataStore:GetAsync("PlayerData")
if success == nil or false then
print("Player is currently nil")
PlayerDataStore:SetAsync("success", true)
PlayerDataStore:SetAsync("PlayerData", HttpService:JSONEncode({deaths = 0, sec = 0, stages = 0}))
Player:Kick("Rejoin, you have no data")
elseif success == true then
local decoded = HttpService:JSONDecode(PlayerData)
-- for k,v in pairs(decoded) do
-- print(k, v, type(v))
-- end
Player.leaderstats.Deaths.Value = decoded.deaths
Player.leaderstats.Seconds.Value = decoded.sec
Player.leaderstats.Stage.Value = decoded.stages
print(success)
print("Completed.")
warn("Continuing with normal RunTime")
Player.CharacterAdded:connect(function(Character)
local humpart = Character.HumanoidRootPart
humpart.CFrame = workspace.Checkpoints:FindFirstChild(stage.Value).CFrame
local d = true
Character:WaitForChild("Humanoid").Died:Connect(function()
humpart.CFrame = workspace.Checkpoints:FindFirstChild(stage.Value).CFrame
if d then
Player.leaderstats.Deaths.Value += 1
end
end)
while wait(3.5) do
local g = true
if g then
Player.leaderstats.Seconds.Value += 1
if ga == true then
break
end
g = false
end
end
end)
end
end)
Players.PlayerRemoving:Connect(function(Player)
ga = true
local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
warn(string.format("%s IN QUEUE...", Player.Name:upper()))
local death = Player.leaderstats.Deaths.Value
local secs = Player.leaderstats.Seconds.Value
local stagess = Player.leaderstats.Stages.Value
local data = {deaths = death,sec = secs, stage =stagess }
local encoded = HttpService:JSONEncode(data)
PlayerDataStore:SetAsync("PlayerData", encoded)
print("Done")
end)
When I use that script the stage still stays at 1. Even though I tried changing the number it doesn’t make a difference.