Help with data stores

i was trying to make a way to store the collectibles in my game but i dont know anything about scripting
i made this using a tutorial on data stores but it doesnt seem to work

local currencyName = "Collectibles"
local Players = game.Players

local DataStoreService = game:GetService("DataStoreService")
local CollectiblesStore = DataStoreService:GetDataStore("CollectiblesStorage")

local function loadData(player)
	local ID = currencyName.."-"..player.UserId
	local data = nil

	local success, err = pcall(function()
		data = CollectiblesStore:GetAsync(ID)
	end)

	if data ~= nil then
		local CollectiblesValue = player.leaderstats:FindFirstChild(currencyName)
		if CollectiblesValue then
			CollectiblesValue.Value = data
			print("Data successfully loaded!")
		end		
	else
		local CollectiblesValue = player.leaderstats:FindFirstChild(currencyName)
		if CollectiblesValue then
			CollectiblesValue.Value = 0
			print("New Player was given 100 Collectibles for joining the Game!")
		end		
	end	
end

local function saveData(player)

	local ID = currencyName.."-"..player.UserId

	local success, err = pcall(function()
		local CollectiblesValue = player.leaderstats:FindFirstChild(currencyName)
		if CollectiblesValue then
			CollectiblesStore:SetAsync(ID, CollectiblesValue.Value)
		end
	end)

	if success then
		print("Successfully saved Player Data for Collectibles")
	else
		print(err)
	end	
end

game:BindToClose(function()
	for i, player in pairs(Players:GetPlayers()) do
		if player then
			player:Kick("Game is shutting down!")
		end
	end

	wait(1)
end)

Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)

What doesn’t work exactly? Also for bind to close you should save player’s data instead of kicking them

its not saving the players data

If your testing in studio make sure to save data in bind to close. All you do is loop through all players and save data.

sry what is bind data i just found this code from a tutorial

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		task.spawn(SaveData, player)
	end
end)

If your serious about your game I would recommend learning datastores they are essential to making a game.

1 Like

do you know anywhere i could learn about them

Tutorials are fine, but I would look into ProfileService and datastore2. You can look into the code they use and may help you understand. Though you learn a lot from just doing it yourself.

1 Like

tutorials usally dont work as well since you have to save different things and the tutorial might not be saving the same thing i am right? so its just better to learn how to do it

i dont know anything about scripting so im not sure how im going to do this

I would recommend starting from the basics and work your way up.