I have been researching DataStores for a bit and I have to say the process is confusing to say the least. All the videos and forum posts I have watched/read through have helped steer me in the right direction but I am still unsure how to set it up without ruining my script that I already have put together. For reference this is my script that I have below:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local TweenTime = TweenInfo.new(1)
local Checkpoints = workspace:WaitForChild("Checkpoints"):GetChildren()
local SoundService = game:GetService("SoundService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ObbyDataStore")
local SoundTemplate = Instance.new("Sound")
SoundTemplate.SoundId = "rbxassetid://6979299092"
SoundTemplate.Volume = .2
SoundTemplate.Pitch = 1
SoundTemplate.RollOffMaxDistance = 20
SoundTemplate.RollOffMinDistance = 10
local function FindCheckpoint(character, stage)
for i, checkpoint in pairs(Checkpoints) do
local checkpoint_number = tonumber(checkpoint.Name)
if stage.value == checkpoint_number then
return checkpoint
end
end
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = 0
stage.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
local root_part = character:WaitForChild("HumanoidRootPart")
local checkpoint = FindCheckpoint(character, stage)
RunService.Heartbeat:Wait()
root_part.CFrame = checkpoint.CFrame * CFrame.new(0,1,0)
end)
end)
for i, checkpoint in pairs(Checkpoints) do
local sound = SoundTemplate:Clone() -- Clone a sound object for each checkpoint
sound.Parent = checkpoint -- Attach the sound to the checkpoint
checkpoint.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid.Health > 0 then
local player = Players:GetPlayerFromCharacter(touch.Parent)
if player then
local leaderstats = player:WaitForChild("leaderstats")
local stage = leaderstats.Stage
local checkpoint_number = tonumber(checkpoint.Name)
if checkpoint_number - stage.Value == 1 then
stage.Value = checkpoint_number
-- Play the sound effect when the checkpoint is touched
sound:Play()
local goal = {
Size = checkpoint.Size + Vector3.new(2, 0.5, 2),
Transparency = 1
}
local checkpointclone = checkpoint:Clone()
checkpointclone.Parent = workspace
local tween = TweenService:Create(checkpointclone, TweenTime, goal)
tween:Play()
tween.Completed:Connect(function()
checkpointclone:Destroy()
end)
local soundClone = sound:Clone()
soundClone.Parent = checkpointclone
local soundConnection = soundClone.Playing:Connect(function()
if soundClone.Playing then
soundClone:Stop()
soundClone:Disconnect()
end
end)
soundClone:Play()
end
end
end
end)
end
This script holds the checkpoints as well as the checkpoint sounds/effects. But, I want to have it where, if you leave the game and you’re at a certain checkpoint, it’ll save the data so when you come back, you’ll be back to where you left off at. Any help is much appreciated!