Help with Datastores

Hello, I need help with my saving script, I’ve already enabled HTTP Services and API Services although it still won’t save/load any data. Feel free to have a look at my code below and try to help me out.

Code
local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = player
	
	while wait(5) do
		coins.Value = coins.Value + 1000
	end

	local data
	local success, errormessage = pcall(function()
		data = datastore:GetAsync(player.UserId)
	end)

	if success then
		coins.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	datastore:SetAsync(player.UserId, player.Coins.Value)
end)

I’m looking forward to script a saving script to save users coins although, this is not working and I’d love if anyone could help me.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

You’re adding a while loop, a while loop will YIELD the script, therefore the value of “Coins” isn’t being set, because the script is yielding. Instead, use coroutines or the task library.

1 Like

Like @10kgoldxdrip said, the loop yields the script
Basically nothing will run under it unless the loop is stopped
So maybe move the loop under all of the other code, like this

local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = player

	local data
	local success, errormessage = pcall(function()
		data = datastore:GetAsync(player.UserId)
	end)

	if success then
		coins.Value = data
	end

    while wait(5) do
		coins.Value = coins.Value + 1000
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	datastore:SetAsync(player.UserId, player.Coins.Value)
end)
1 Like

This method below works, in which I know because I have tested it multiple times.

You basically just use a table to store all of the players coins and then when someone leaves it will save the table for that user. This is the method I personally use for storing data in my games.

local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("Coins")
local Coins = {}

game.Players.PlayerAdded:Connect(function(Plr)
	Coins[Plr.UserId] = datastore:GetAsync(Plr.UserId)
	if Coins[Plr.UserId] == nil then
		Coins[Plr.UserId] = 0
	end
	while wait(5) do
		Coins[Plr.UserId] += 1000
	end

end)


game.Players.PlayerRemoving:Connect(function(Plr)
	datastore:SetAsync(Plr.UserId, Coins[Plr.UserId])
end)

Good luck! And, if for whatever reason it doesn’t just reply to this message and I can troubleshoot it more.

1 Like
local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = player
	
spawn(function()
	while wait(5) do
		coins.Value = coins.Value + 1000
	end
end)
	local data
	local success, errormessage = pcall(function()
		data = datastore:GetAsync(player.UserId)
	end)

	if success then
		coins.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	datastore:SetAsync(player.UserId, player.Coins.Value)
end)
1 Like

Thank you, it has been solved already but I’ll make sure to try this out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.