DataStore and value changing problem?

Sup

I have a TextButton, leaderstats and their DataStore system.
Leaderstats have one child: Money

If I’ll click the TextButton the value of Money will decrease by 1

The value is decreasing but if i’m leaving the game the Money isn’t saving
The DataStore system isn’t working or the TextButton?

TextButton Script:

repeat wait() until game.Players.LocalPlayer
local plr = game.Players.LocalPlayer

plr.PlayerGui.ScreenGui.decrease.MouseButton1Click:Connect(function()
local money = plr.leaderstats.Money
money.Value = money.Value-1

print("Value:".. money.Value)

end)

Location of TextButton: game.StarterGui.ScreenGui.decrease
The TextButton script is inside the TextButton

DataStore/leaderstats system script:

local DS = game:GetService(“DataStoreService”):GetDataStore(“leaderstats”)
game.Players.PlayerAdded:Connect(function (plr)
local stats = Instance.new(“Folder”,plr)
stats.Name = “leaderstats”

local money = Instance.new("IntValue",stats)
money.Name = "Money"

local key = "key-".. plr.UserId
if DS:GetAsync(key) then
	money.Value = DS:GetAsync(key)
else
	DS:SetAsync(key,0)
	money.Value = DS:GetAsync(key)
end

end)
game.Players.PlayerRemoving:Connect(function (plr)
local key = “key-”… plr.UserId
DS:SetAsync(key,plr.leaderstats.Money.Value)
end)

The DataStore/leaderstats system script is inside ServerScriptService

What i’m doing wrong? It’s the TextButton not working or DataStore?
(What i want: When i click the TextButton the value of Money will decrease by 1 and when I’ll this value will save)

Please help me.

I think you are misunderstanding what a client-server model is. Clearly you’ve been changing the value from a client script, also known as LocalScript. No values are changed on the server if performed from client.


Possible alternatives:

  • Use a RemoteEvent.
  • Connect the listener on server.

On a side note, your current saving and loading methods are not to be relied on entirely. You should really read about pcalls which are short for protected calls. Usually helps catching errors from any unexpected on DataStore.

4 Likes

I recommend reading the wiki articles Roblox Client-Server Model and Remote Functions and Events to get a better understanding of the cause of your issue and how to handle it.

1 Like