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.