Datastore not saving

  1. What do you want to achieve? Keep it simple and clear!
    I want the coins & rebirths value saved

  2. What is the issue?
    They do not save. Its printing out “Saved” but not acttualy saving it

  3. What solutions have you tried so far?

I turned on the API. and i think the problem may be from the script that is adding to the stats (Its a server side script)

Data Store code

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("DataMarcel") 

local repStorage = game:GetService("ReplicatedStorage")
local events = repStorage:FindFirstChild("Events")
local AddCoins = events:FindFirstChild("AddCurrency")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"

	local currency = Instance.new("NumberValue", folder)
	currency.Name = "Coins" 
	currency.Value = 0

	local rebirth = Instance.new("NumberValue",folder)
	rebirth.Name = "Rebirths"
	rebirth.Value = 0


	local PlayerId = plr.UserId

	local data
	local succes, errormsg = pcall(function()
		data = ds:GetAsync(PlayerId)
	end)

	if succes then
		if data then
			currency.Value = data.Coins
			rebirth.Value = data.Rebirths
		end
	else
		currency.Value = 0
		rebirth.Value = 0
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	local PlayerId = plr.UserId

	local data = {
		Cash = plr.leaderstats.Coins.Value;
		Rebirths = plr.leaderstats.Rebirths.Value;
	}

	local succes, errormsg = pcall(function()
		ds:SetAsync(PlayerId,data)
	end)

	if succes then
		print("Saved")
	end

end)

game:BindToClose(function()
	wait(2)
end)


The script where is updating the data

local coinspawn = 0
local limit = false

while true do
	wait(0.5)
	if limit == true then
        
	else
		coinspawn += 1
		local Coiner = Instance.new("Part", workspace)
		Coiner.Position = Vector3.new(math.random(5,50), 5, math.random(5,50))
		Coiner.Color = Color3.fromRGB(170, 170, 0)
		Coiner.Size = Vector3.new(0.15,3,3)
		Coiner.Shape = "Cylinder"
		Coiner.Material = Enum.Material.Metal
		local detector = Instance.new("ClickDetector", Coiner)
		detector.MouseClick:Connect(function(player)
			local pcoin = player.leaderstats.Coins
			local lrebirth = player.leaderstats.Rebirths
			if lrebirth.Value == 0 then
				pcoin.Value += 1
			elseif lrebirth.Value > 0 then
				pcoin.Value += 1 * lrebirth.Value + 1
			end
			coinspawn -= 1
			Coiner:Destroy()
		end)
	end
	if coinspawn == 50 then
	limit = true
	elseif coinspawn < 1 then
		limit = false
	end
end

currency.Value = data.Coins but

local data = {
		Cash = plr.leaderstats.Coins.Value;
		Rebirths = plr.leaderstats.Rebirths.Value;
	}

Replace Cash to Coins

If some key doesn’t exist, it will return nil instead of erroring, the success check you did there is completely useless, since it would just wipe player data if GetAsync would actually error.

Thanks! You really helped me out

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