Im having trouble with DataStores

Hi, I am very new to scripting datastores. I have been avoiding them a lot because I don’t really understand it until today where I tried to make one

so basically this script is supposed to save an Int value which is their currency
(I am not making a leaderstats) but It doesn’t save
The Code:

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("CoinsData")

game.Players.PlayerAdded:Connect(function(player)
	local Coins = Instance.new("IntValue", player)
	Coins.Name = "Coins"
	Coins.Value = Data:getAsync(player.UserId) or 0
	Data:setAsync(player.UserId, Coins.Value)
	game.Players.PlayerRemoving:Connect(function()
		Data:setAsync(player.UserId, Coins.Value)
	end)
end)

Ty for the help : )

This should not be in the PlayerAdded:Connect() function. This should be a separate function.

It is very poor practice to have the PlayerRemoving event handler inside of the PlayersAdded event handler because it will create a new connection for each player, and that connection will fire for any player that leaves. Your solution?

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("CoinsData")

game.Players.PlayerAdded:Connect(function(player)
	local Coins = Instance.new("IntValue", player)
	Coins.Name = "Coins"
	Coins.Value = Data:GetAsync(player.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(player)
	Data:SetAsync(player.UserId, Coins.Value)
end)

It is also very helpful to add a connection to the game’s Close event, in case of server-shutdowns and what-not. Learn more about Close here.

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("CoinsData")

game.Players.PlayerAdded:Connect(function(player)
	local Coins = Instance.new("IntValue", player)
	Coins.Name = "Coins"
	Coins.Value = Data:getAsync(player.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(player)
        local Coins = player:FindFirstChild("Coins");
	Data:GetAsync(player.UserId, Coins and Coins.Value or 0) -- Set to Coins.Value or 0 by default
end)

game:BindToClose(function()
-- save everyone's data
    for i,v in pairs(game.Players:GetChildren()) do
        if(v:FindFirstChild("Coins")) then
            Data:SetAsync(v.UserId, v.Coins.Value);
        end
    end
end)
1 Like

Thank you for the help but It still doesn’t save the player’s data and studio API service is enabeled.

and `````````````````````````````````

oops wrong replies :neutral_face:
sorry

Use game:BindToClose(function() instead of game.Close.

Good catch. I’ve updated the snippet.

1 Like

There are several things wrong with your code. Since you’re new to data stores that’s understandable. Roblox has an Onboarding program that gives tutorials in the different aspects of game development. They currently have one tutorial for game data called Saving Data | Roblox Creator Documentation. You should check it out, as it is very helpful.