Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datasave not working to save a player's coin amount?

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

0

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

1 answer

UPDATED !!!

Datastores are very hard, and I can't find anything wrong so here is a very simple datastore

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("WaveSaveSystem")

game.Players.PlayerAdded:connect(function(player)
	
	local coinstats = Instance.new("Folder",player)
	coinstats.Name = "constants"

	local coins = Instance.new("IntValue",constants)
	coins.Name = "Coins"

	
		ds:SetAsync(player.UserId, coinstats.coins)
		
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.coinstats.coins)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.