What do you want to achieve?
I want to have stats in a players leaderstats folder save when they leave and load the stored data when they join.
What is the issue?
The players data seems to save, (their userid is printed, which means it successfully saved) but does not load, and instead just defaults to 0.
What solutions have you tried so far?
I have looked for a very long time on the developer forums and the dev hub, but none of the solutions fixed my problem. (Switching the script, publishing to roblox, enabling and disabling access to datastore, etc).
I have already enabled access to datastores, and I am not getting any errors in console. Anything is appreciated. Please help!
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
local function deserializeData(player, data)
local leaderstats = player.leaderstats
for statName, statValue in next, data do
local stat = leaderstats:FindFirstChild(statName)
if statName then
stat.Value = statValue
end
end
end
local function serializeData(player)
local data = {}
local leaderstats = player.leaderstats
for _, stat in ipairs(leaderstats:GetChildren()) do
data[stat.Name] = stat.Value
end
return data
end
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local robux = Instance.new("IntValue")
robux.Name = "Robux"
robux.Parent = leaderstats
local multiplier = Instance.new("IntValue")
multiplier.Name = "Multiplier"
multiplier.Parent = leaderstats
local success, result = pcall(function()
return datastore:GetAsync("Stats_"..player.UserId)
end)
if success then
if result then
deserializeData(player, result)
end
else
warn(result)
end
end
local function onPlayerRemoving(player)
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
local function onServerShutdown()
for _, player in ipairs(players:GetPlayers()) do
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)
Edit: I also have a second script that only works in one game, but not the other.
Edit 2: I fixed it with a plugin called Datastores!
That’s not working for me, and I think it may be the script of the button since I tried with a different datastore I used for a separate game that uses a tool instead (the tool works with saving data) and it didn’t work. The script for the button is attached below
local gui = script.Parent
local btn1 = gui.RobuxBtn
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local rbx = leaderstats:WaitForChild("Robux")
local multiplier = leaderstats:WaitForChild("Multiplier")
local enabled = true
btn1.MouseButton1Click:Connect(function()
if enabled == true then
if multiplier.Value > 1 then
rbx.Value = rbx.Value + (1 * multiplier.Value)
else
rbx.Value = rbx.Value + 1
end
enabled = false
wait(0.1)
enabled = true
else
wait(0.0001)
end
end)
Tried that and still not loading data
Here’s the changed script
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
local function deserializeData(player, data)
local leaderstats = player.leaderstats
for statName, statValue in next, data do
local stat = leaderstats:FindFirstChild(statName)
if statName then
stat.Value = statValue
end
end
end
local function serializeData(player)
local data = {}
local leaderstats = player.leaderstats
for _, stat in ipairs(leaderstats:GetChildren()) do
data[stat.Name] = stat.Value
end
return data
end
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local robux = Instance.new("IntValue")
robux.Name = "Robux"
robux.Parent = leaderstats
local multiplier = Instance.new("IntValue")
multiplier.Name = "Multiplier"
multiplier.Parent = leaderstats
local success, result = pcall(function()
return datastore:GetAsync("Stats_"..player.UserId)
end)
if success then
deserializeData(player, result)
else
warn(result)
end
end
local function onPlayerRemoving(player)
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
print(result)
else
warn(result)
end
end
local function onServerShutdown()
for _, player in ipairs(players:GetPlayers()) do
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
print(result)
else
warn(result)
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)