Hallo again!
I’m trying to make a way for my game to save how many times they beat an obstacle course. I’ve made a script to make the leaderstats:
local Players = game:GetService("Players")
local DataStoreSevice = game:GetService("DataStoreService")
local winstore = DataStoreSevice:GetDataStore("PlayerWins")
local function setupleaderboard(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats
end
--Trigger
game.Players.PlayerAdded:Connect(setupleaderboard)
A script to increase their wins by 1 when they reach the end:
local Players = game:GetService("Players")
local winners = {}
local function OnHit(object)
local objectparent = object.Parent
local h = object.Parent:FindFirstChild("Humanoid")
if h then
object.Parent.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(13.65, 2.8, -5.05))
local player = Players:GetPlayerFromCharacter(objectparent)
local leaderstats = player.leaderstats
local winstat = leaderstats and leaderstats:FindFirstChild("Wins")
if winstat then
winstat.Value = winstat.Value + 1
table.insert(winners, objectparent)
end
end
end
script.Parent.Touched:Connect(OnHit)
And a script to make a datastore. (Mostly copied from DevHub :P)
-- Set up table to return to any script that requires this module script
local PlayerStatManager = {}
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
-- Table to hold player information for the current session
local sessionData = {}
local AUTOSAVE_INTERVAL = 60
-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
local playerUserId = "Player_" .. player.UserId
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
else
sessionData[playerUserId][statName] = value
end
end
-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
local playerUserId = "Player_" .. player.UserId
local success, data = pcall(function()
return playerData:GetAsync(playerUserId)
end)
if success then
if data then
-- Data exists for this player
sessionData[playerUserId] = data
else
-- DataStore is working, but this player is new.
sessionData[playerUserId] = {Wins = 0}
end
else
warn("Cannot access data store.")
end
end
local function savePlayerData(playerUserId)
if sessionData[playerUserId] then
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end)
if not success then wait(1) end
until tries == 3 or success
if not success then
warn("Cannot save data!")
end
end
end
local function SaveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId)
end
local function autoSave()
while wait (AUTOSAVE_INTERVAL) do
for playerUserId, data in pairs(sessionData) do
savePlayerData(playerUserId)
end
end
end
-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(SaveOnExit)
return PlayerStatManager
How do I save the leaderstat “Wins” to the datastore, so when a player leaves and rejoins, they have the same amount of wins as before? Sorry if I’m doing this wrong, I’m a new developer.