You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
- The script is supposed to load leaderstats values for points and wins saved to a DataStore and make them show up on the leaderboard as the correct value for each player.
- What is the issue? Include screenshots / videos if possible!
The script only loads the first value it was asked to load, making the second value show up as 0 in the leaderboard, thus autosaving that value as 0 and wiping any data the player may have had there.- Update: The problem actually seems to be that the data isn’t successfully saved even when it says it is.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Switching the values around only makes it so that the other value isn’t loaded. I could not find anyone with similar issues. Can there only be one playerstats value per player? Should I be using a table?- Currently trying to figure out what’s going on
Here’s the problem script. I’ve marked the bug with a comment. This is my first time working with DataStores, please help me figure out what I did wrong here.
--[[
Datastore names:
Points: UserId-points
Wins: UserId-wins
]]
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("datastore")
local function savedata(player)
local success, errormessage = pcall(function()
datastore:SetAsync(player.UserId.."-points",player.leaderstats.points.Value)
print(datastore:SetAsync(player.UserId.."-points",player.leaderstats.points.Value))
datastore:SetAsync(player.UserId.."-wins",player.leaderstats.wins.Value)
print(datastore:SetAsync(player.UserId.."-wins",player.leaderstats.wins.Value))
end)
if success then
print("data successfully saved for " .. tostring(player))
else
print("error saving data for " .. tostring(player) .. ". They left, so their progress is lost.")
warn(errormessage)
end
end
local function loadstats(stat,player)
local leaderstat = nil
local success, errormessage = pcall(function()
leaderstat = datastore:GetAsync(player.UserId.."-"..tostring(stat))
print(tostring(stat))
print(player.UserId.."-"..tostring(stat))
print(leaderstat)
end)
if success then
stat.Value = leaderstat
print("successfully recieved leaderstats data for " .. tostring(player))
else
print("error recieving " .. tostring(player) .. "'s leaderstats data")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(function(player) --Fires when players are added, loads their playerstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "points"
points.Value = 10
local wins = Instance.new("IntValue")
wins.Name = "wins"
wins.Value = 1
--BUG: only the first stat called actually loads, the second stat will always show up as the default value (0)
loadstats(points,player)
loadstats(wins,player)
points.Parent = leaderstats
wins.Parent = leaderstats
end)
-----------------------------VVV-- Saving Data --VVV------------------------------------
game.Players.PlayerRemoving:Connect(function(player) --Fires when players leave, saves their playerstats
savedata(player)
end)
--[[
game:BindToClose(function() --Fires when the server is about to close
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("Server shutting down, try again later")
end
end
wait(5)
end)
]]
-- Uncomment ^^this^^ for the full release to protect data loss on server shutdown
while true do --Autosaves playerstats for all players every few seconds
wait(45)
for i, player in pairs(game.Players:GetPlayers()) do
if player then
savedata(player)
end
end
end