Having trouble with some data saving!

Would there happen to be any errors/warnings within your Output at all? And what kind of script is it?

1 Like

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?

image

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?

1 Like

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!

3 Likes

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
1 Like

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!

1 Like

I’m not that huge on plugins, but I think I did find something that could possibly help you with it:

1 Like

Alright, thank you so much again, you really made my day!