Data Store script not working

Hello, So me and my friend is working on a game and we ran into a new problem
with our scripts.
We got a working data store script but the only problem with it is in our game we have a shop that - 10 for each item brought but the only problem is that when we - the cash it doesn’t update the data store.
What I mean is when we buy a item and leave the game and rejoin we still have the same amount of cash before we brought the item.

Here are the scripts.

– Data store –
local DataStoreService = game:GetService(“DataStoreService”)
local CoinsStore = DataStoreService:GetDataStore(“CoinsStore”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder") --making leaderstats
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Coins = Instance.new("IntValue") --making coins
Coins.Name = "EndCoins"
Coins.Parent = leaderstats



local UserId = player.UserId

local data

local success, errormessage = pcall(function() --getting loaded data
	data = CoinsStore:GetAsync(UserId)
end)

if success then
	Coins.Value = data
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
local data = player.leaderstats.Coins.Value

CoinsStore:SetAsync(UserId, data)

end)

– Give coins –
local function CharacterAdded(plr, char)
local hum = char:WaitForChild(“Humanoid”)
local leaderstats = plr:WaitForChild(“leaderstats”)
local coins = leaderstats:WaitForChild(“EndCoins”)

if not leaderstats or not coins then return end

hum.Died:Connect(function()
	coins.Value += 5
end)

end

game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character

if char then
	CharacterAdded(plr, char) -- character may load before the event
end

plr.CharacterAdded:Connect(function(char)
	CharacterAdded(plr, char)
end)

end)

–Buy items–
local player = game.Players.LocalPlayer
local Model = workspace:WaitForChild(“MissStopButton”)
local del = game.Workspace.Button
local del3 = script.Parent.Parent.Parent.Shovel.Shovel.ShovelShow

local Rotation = table.pack(Model:GetPivot():ToOrientation())

script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.EndCoins.Value >= 25 then
player.leaderstats.EndCoins.Value = player.leaderstats.EndCoins.Value - 25
del:Destroy()
Model:PivotTo(CFrame.new(210.5, 2.875, -166.5) * CFrame.fromOrientation(table.unpack(Rotation)))
del3:Destroy()
script.Parent.ChangeStop:Destroy()
end
end)

I don’t know the problem but I think it might be the data store not updating if I’m right please say how I can fix this.

Try not using SetAsync when saving data:

make sure api service on

Yes I have got that on my problem us that the data isn’t saving.

You need to fire RemoteEvent every time a players buys an item:

Server:

RemoteEvent.OnServerEvent:Connect(function(player)
     if player.leaderstats.EndCoins.Value >= 25 then
         player.leaderstats.EndCoins.Value -= 25
     end
end)

Client:

local player = game.Players.LocalPlayer
local Model = workspace:WaitForChild(“MissStopButton”)
local del = game.Workspace.Button
local del3 = script.Parent.Parent.Parent.Shovel.Shovel.ShovelShow

local Rotation = table.pack(Model:GetPivot():ToOrientation())

script.Parent.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer()
    del:Destroy()
    Model:PivotTo(CFrame.new(210.5, 2.875, -166.5) * CFrame.fromOrientation(table.unpack(Rotation)))
    del3:Destroy()
    script.Parent.ChangeStop:Destroy()
end)

Your problem is that you are deducting the player’s coins from the client, so they are not updated for the server. That’s why you need to use RemoteEvent for this.

1 Like

you saving nil value

"local data = player.leaderstats.EndCoins.Value" 

not

 "local data = player.leaderstats.Coins.Value"

change it to

local data = player.leaderstats.EndCoins.Value

I just tried and it makes the whole script not work.

I just tried and it changed something but it still doesn’t work.

What do you mean by not work? Buy items script not working or all of them?

The buy item script doesn’t work.

You can try using RemoteFunction in your case.

Server:

RemoteFunction.OnServerInvoke = function(player)
    if player.leaderstats.EndCoins.Value >= 25 then
        player.leaderstats.EndCoins.Value -= 25
        return true
    else
        return false
    end
end

Client:

local player = game.Players.LocalPlayer
local Model = workspace:WaitForChild(“MissStopButton”)
local del = game.Workspace.Button
local del3 = script.Parent.Parent.Parent.Shovel.Shovel.ShovelShow

local Rotation = table.pack(Model:GetPivot():ToOrientation())

script.Parent.MouseButton1Click:Connect(function()
    local result = RemoteFunction:InvokeServer()
    if result == true then
       del:Destroy()
       Model:PivotTo(CFrame.new(210.5, 2.875, -166.5) * CFrame.fromOrientation(table.unpack(Rotation)))
       del3:Destroy()
       script.Parent.ChangeStop:Destroy()
   else
        print("Player does not have enough coins to purchase this item.")
    end
end)

Where would I put these scripts?

The first is on the server and the second in buy items script.

What do you mean by on the sever

Script in ServerScriptService.

One question does the script at least save once in a while?

I believe its when the player joins the game for the first time.

I think you should use a BindToClose function

game:BindToClose(Function()
wait(2)
end)
1 Like

SetAsync works fine for me, I think it’s another problem