I'm trying to make a datasave for a player's coins in my game. Everything is working well (the coins, loading for a new player, ect...) except it doesn't seem to actually save a player's coins. Here is my code, please tell me if you can tell what is wrong.
local DataStore = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
game.Players.PlayerAdded:Connect(function(player)
local coinstats = Instance.new("Folder",player)
coinstats.Name = "coinstats"
local coins = Instance.new("IntValue",coinstats)
coins.Name = "Coins"
local ID = coins.Value.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
coins.Value = savedData
print("Data loaded")
else
--New player
coins.Value = 250
print("New player to the game")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local coins = player.coinstats.coins.Value
local ID = coins.."-"..player.UserId
DataStore:SetAsync(ID,player.coinstats[coins].Value)
end)
game:BindToClose(function()
-- When game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:kick("Server has shutdown")
end
end
wait(5)
end)

1 comment
Make sure API service in enabled go to gamesetting and enable api service.