Ok, I’m back from testing, it doesn’t save. It says on the server-side of the logs that the values have been saved but I rejoin to test and nope, values are at 0.
Unfortunately, some of the scripts you’re attempting to use could potentially work janky in the future & this is probably 1 of the many decent DataStore scripts out there that can do the job well
I’d be willing to break the process though
SetAsync()/GetAsync
yields in an attempt to save/load the Player’s Data, there is the possibly of it erroring though so if you don’t encase it in what’s called a pcall
function (Aka Protective/Protection Call idk), the script could break entirely
Kinda think of a pcall
as like preventing scripts from erroring, & say we wanted to search for a Part named “Billy” inside the workspace (But there wasn’t one, and instead we have a Part named “Steve”)
local Billy --Hi Billy!--
local success, errormessage = pcall(function()
Billy = workspace:FindFirstChild("Billy") --Attempting to find "Billy"
end)
if not success then
warn("Billy's disappeared!") --A part named "Billy" was unable to be searched inside the workspace
end
Ok thanks for the explanation, I’ll use your script right away and come back to tell the results.
I am back, it took me a while but sadly it doesn’t work…
Would there happen to be any errors/warnings within your Output at all? And what kind of script is it?
No errors, no outputs for saving, only 2 for successfully loading my data. The script is a normal one (not a local one) since that’s what I was using and it is located in ServerScriptService. Should I use a local one since the server has to access the player in specific?
Idk what you’re doing differently, cause it’s working fine for me
How are you changing the value exactly? Is it on the client, or the server?
this should work:
local dss = game:GetService("DataStoreService")
local players = game:GetService("Players")
local ds = dss:GetDataStore("PlayerStore")
local function setUp(player)
local userId = player.UserId
local key = "Player_"..userId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "TotalGames"
local data = ds:GetAsync(key)
cash.Value = data or 0
cash.Parent = leaderstats
leaderstats.Parent = player
end
local function saveUp(player)
local userId = player.UserId
local key = "Player_"..userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats.Cash.Value
ds:SetAsync(key, cashValue)
end
end
local function onShutDown()
wait(1)
end
game:BindToClose(onShutDown)
players.PlayerAdded:Connect(setUp)
players.PlayerRemoving:Connect(saveUp)
To add/take from the value I do with a local script:
local games = game.Players.LocalPlayer.leaderstats.TotalGames.Value
games = games + 1
As I said when I created the topic, I have multiple values, “doomcoins” and “totalgames”. I store “doomcoins” in “leaderstats” and “totalgames” in “leaderstats2”.
Aha, that explains it!
The issue here is that you’re changing the value from the Local’s standpoint, which does not replicate to the server (Which is why the Data keeps loading 0 every time you attempt to run it)
What we can use then, is something called a RemoteEvent
to properly add a Value from the server’s standpoint
We’ll place it inside ReplicatedStorage
so that it’ll be able to easily be replicated across both sides (Server/Client):
-- LocalScript
local Plr = game.Players.LocalPlayer
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
-- Do your stuff here, but DO NOT add the Value from this side
Event:FireServer()
-- Main Script (ServerScriptService)
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
Event.OnServerEvent:Connect(function(Plr)
local TotalGames = Plr:WaitForChild("leaderstats"):WaitForChild("TotalGames")
TotalGames.Value += 1
end)
Now this should work hopefully!
so what exactly is the problem? Does the data not save or something?
Sorry I’m just bad at reading stuff xD, apologies if I don’t understand.
And the
Event.OnServerEvent:Connect(function(Plr)
local TotalGames = Plr:WaitForChild("leaderstats"):WaitForChild("TotalGames")
TotalGames.Value += 1 --(Btw what does this do)
end)
I just put it in the main script on a new line or it needs to be implemented in a specific part of the script?
are you even listening to what im saying? im trying to help you.
Just put it as a Script
inside ServerScriptService
This is just a simpler way of adding a value
It’s pretty much the same as:
TotalGames.Value = TotalGames.Value + 1
There’s just no difference really
TotalGames.Value += 1
Yea, my game isn’t saving the values. Check the original script that I posted when I created the topic.
Ok. It think the problem because your not using BindToClose(), I usually make this mistake aswell
maybe try something like this:
local function onShutDown()
local userId = player.UserId
local key = "Player_"..userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats.Cash.Value
ds:SetAsync(key, cashValue)
end
end
game:BindToClose(onShutDown)
if you want. You can copy and past my code into the script
local dss = game:GetService("DataStoreService")
local players = game:GetService("Players")
local ds = dss:GetDataStore("PlayerStore")
local function setUp(player)
local userId = player.UserId
local key = "Player_"..userId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "TotalGames"
local data = ds:GetAsync(key)
cash.Value = data or 0
cash.Parent = leaderstats
leaderstats.Parent = player
end
local function saveUp(player)
local userId = player.UserId
local key = "Player_"..userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats.Cash.Value
ds:SetAsync(key, cashValue)
end
end
local function onShutDown()
local userId = player.UserId
local key = "Player_"..userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats.Cash.Value
ds:SetAsync(key, cashValue)
end
end
game:BindToClose(onShutDown)
players.PlayerAdded:Connect(setUp)
players.PlayerRemoving:Connect(saveUp)
this all works fine for me
you still having trouble with the script?
I’m back with good news. I think it’s fixed. I just have to readjust the values since I messed up with them. The values pop up as they should on the studio. Thanks a lot, you are a genius (+rep)! Btw do you know a plugin or something where I could see and edit everyone’s data? Would really appreciate that since it is useful for those situations.
And also a huge thank you to the others that participated and helped!
I’m not that huge on plugins, but I think I did find something that could possibly help you with it:
Alright, thank you so much again, you really made my day!