Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my datastore script not storing data?

Why is my datastore showing that money has been added, but doesn't really add?

Here is an image of what i'm talking about.

https://imgur.com/a/RtZxb7h

Here is the datastore script i'm using.

local DSService = game:GetService("DataStoreService"):GetDataStore("Leaderstats")

game:GetService("Players").PlayerAdded:Connect(function(plr)
local uniquekey = ("id-"..plr.UserId)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr

local savevalue = Instance.new("IntValue")
savevalue.Name = "Money"
savevalue.Value = 500
savevalue.Parent = leaderstats

  

local savevalue2 = Instance.new("IntValue")
savevalue2.Name = "Gems"
savevalue2.Value = 0
savevalue2.Parent = leaderstats

  

local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
savevalue.Value = GetSaved[1]
savevalue2.Value = GetSaved[2]
else
local NumbersForSaving = {savevalue.Value, savevalue2.Value}
DSService:SetAsync(uniquekey, NumbersForSaving)
end
end)

  
game:GetService("Players").PlayerRemoving:Connect(function(plr)
local uniquekey = ("id-"..plr.UserId)
local Savetable = {plr.leaderstats.Money.Value, plr.leaderstats.Gems.Value}
DSService:SetAsync(uniquekey, Savetable)
end)

4 comments

0

Doesn't really help us much, could you explain by what you mean for "doesn't really add"?

0

Did you check the photo? When money is added. It shows the money is added, but it isn't as you can see in the photo. When you print the money value it is the same as before!

0

Are you changing the money stat on the client?

0

Yes.

1 answer

✓ Accepted answer

Explanation

Since you are changing the stat on the Client, due to FilteringEnabled, it will not replicate to the Server. As a result, you will need to use a Remote Event to give the players their correct stat values.

Example Server Script

local remote = Instance.new("RemoteEvent")
remote.Name = "SaveValueEvent"
remote.Parent = game:GetService("ReplicatedStorage")

remote.OnServerEvent:Connect(function(plr, stat, val)
	if stat == "Money" then
		plr:WaitForChild("leaderstats"):WaitForChild("Money").Value = val
	elseif stat == "Gem" then
		plr:WaitForChild("leaderstats"):WaitForChild("Gems").Value = val
	end
end)

Example Local Script

local remote = game:GetService("ReplicatedStorage"):WaitForChild("SaveValueEvent")
local plr = game:GetService("Players").LocalPlayer
local money = plr:WaitForChild("leaderstats"):WaitForChild("Money")
local gem = plr:WaitForChild("leaderstats"):WaitForChild("Gems")

money.Changed:Connect(function()
    remote:FireServer("Money", money.Value)
end)

gem.Changed:Connect(function()
    remote:FireServer("Gem", gem.Value)
end)

The above LocalScript simply sends the Server information when the values change on the Client (although this really should be done completely on the Server through the RemoteEvent)

Once the information is sent, the Server can then alter the leaderstat values, where they will be saved by your DataStore when the player leaves.

This discussion is preserved as it was. New answers, comments, and votes are closed.