Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my datastore script not storing data?

Asked by 5 years ago

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)
0
Doesn't really help us much, could you explain by what you mean for "doesn't really add"? Ziffixture 6913 — 5y
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! kittonlover101 201 — 5y
0
Are you changing the money stat on the client? SerpentineKing 3885 — 5y
0
Yes. kittonlover101 201 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

Ad

Answer this question