So I’ve created a currency system in my game, and made it so the data would save every time the player would leave the game.
But this isn’t the case, it doesn’t work and I am not sure why.
I’ve tried many things like enabling API services and it still will not work!
Any help would be awesome, as I really do not want to restart this work!
Thank you!
Here’s the currency and datastore lua
local DS = game:GetService("DataStoreService")
local cashStore = DS:GetDataStore("Mooneey")
local remote = game:GetService("ReplicatedStorage").CurrencyRemote.GiveCurrency
game.Players.PlayerAdded:Connect (function(player)
local cashValue
local success, err = pcall(function()
cashValue = cashStore:GetAsync("Player_"..player.UserId
)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
if success then
Cash.Value = cashValue
else
print("Failed To Save Data, Sorry")
end
end)
local function save (player)
local success, err = pcall (function()
cashStore:GetAsync("Player_"..player.UserId, player.leaderstats.Cash.Value)
end)
if success then
print("Your Data Has Saved")
else
print("Failed To Save Data2, Sorry")
end
end
local function autosave()
while wait(10) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save (player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Cash.Value += amount
end)
spawn(autosave)