Leaderstats
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoined(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Stage = Instance.new("NumberValue")
Stage.Parent = leaderstats
Stage.Name = "Stage"
Stage.Value = 0
local PlayerUserID = "Player_"..player.UserId
local data = PlayerData:GetAsync(PlayerUserID)
if data then
Stage.Value = data["Stage"]
else
Stage.Value = 0
end
end
local function Create_Table(player)
local player_table = {}
for _,Val in pairs(player.leaderstats:GetChildren()) do
player_table[Val.Name] = Val.Value
end
return player_table
end
local function onPlayerExit(player)
local player_stats = Create_Table(player)
local success, err = pcall(function()
local playerUserId = "Player_"..player.UserId
PlayerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn(player.Name.."'s data could not be saved!")
end
end
game.Players.PlayerAdded:Connect(onPlayerJoined)
game.Players.PlayerRemoving:Connect(onPlayerExit)TouchedScript
local Players = game:GetService("Players") -- Player Service
Players.PlayerAdded:Connect(function(plr) -- Function when player loads into the game
script.Parent.Touched:Connect(function(plr) -- The parent of this script is touched
script.Parent.Parent.StageChange:Fire(plr.Parent.Name, script.Parent.Name) -- Firing bindable event with the player name and the name of the part
print("Fired")
end)
end)PlayerSpawner
local Players = game:GetService("Players") -- Player Service
Players.PlayerAdded:Connect(function(plr) -- Function when player loads into the game
plr.CharacterAdded:Connect(function() -- Function when player's character loads into the game
local Stage = plr:WaitForChild("leaderstats").Stage.Value -- Finding stage value
local player = game.Workspace:WaitForChild(plr.Name) -- Finding player in workspace
local function respawn() -- Creating a function
print(plr.Name.." Is At Stage "..Stage)
if Stage == 0 then -- Checking stage
else
local hrp = player:WaitForChild("HumanoidRootPart") -- Finding HumanoidRootPart
hrp.CFrame = CFrame.new(workspace.SavingCheckpoints:FindFirstChild(tostring(Stage)).Position) + Vector3.new(0,5,0) -- Teleporting player to current checkpoint
end
end
respawn() -- Calling the function, which will now run every time the character loads in
end)
end)
StageUpdater
local Players = game:GetService("Players") -- Player Service
script.Parent.StageChange.Event:Connect(function(plr, stage) -- Function that runs when the bindable event is fired
local player = Players:FindFirstChild(plr) -- Finding player in Players Folder
local leaderstats = player:FindFirstChild("leaderstats") -- Finding Leaderstats Folder
if leaderstats.Stage.Value < tonumber(stage) then -- Checking if player is eligible for the stage
leaderstats.Stage.Value = tonumber(stage) -- Updating player stage
end
end)