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

Why coins (dataStorage) aren't saving when player leave the game?

i cant save the data when the player leave the game, but every time i try to save the player coins the function dont work :(

local DataStoreService = game:GetService("DataStoreService")

local dataCentral = {
	coins = 0,
}

local datas = {}
local canRun = false
function loadData(player)
	datas[player] = {}
	datas[player].coins = 0
	for i, v in pairs(dataCentral) do
		if not DataStoreService:GetDataStore(i):GetAsync(tostring(player.UserId)) then
			DataStoreService:GetDataStore(i):SetAsync(tostring(player.UserId), v)
		else
			dataCentral[i] = DataStoreService:GetDataStore(i):GetAsync(tostring(player.UserId))
			datas[player][i] = dataCentral[i]
			warn(DataStoreService:GetDataStore('coins'):GetAsync(tostring(player.UserId)))
		end
	end
	
		
	local stat = Instance.new("IntValue")
	stat.Name = "leaderstats"
	local cash = Instance.new("IntValue")
	cash.Name = "Coins"
	cash.Value = dataCentral.coins
	cash.Parent = stat
	stat.Parent = player
	
	if not canRun then
		canRun = true
		check()
	end
end 

function check()
	while true do
		for i, v in pairs(game.Players:GetChildren()) do
			if v.leaderstats.Coins.Value ~=  DataStoreService:GetDataStore('coins'):GetAsync(tostring(v.UserId)) then
				if v.leaderstats.Coins.Value ~= datas[v].coins then
					datas[v].coins = v.leaderstats.Coins.Value
				end				
			end
		end
		wait()
	end
end

function saveData(player)
	DataStoreService:GetDataStore('coins'):SetAsync(tostring(player.UserId), datas[player].coins)

end

game:GetService("Players").PlayerAdded:connect(loadData)
game:GetService("Players").PlayerRemoving:connect(saveData)

The error is in the function saveData()

4 comments

0

:Connect not :connect connect is deprecated

0

that won't cause it to not work though

0

can you use an object as a position in an array? maybe try datas[player.Name] and see if that works(I am new to arrays)

0

nothing works :(

1 answer

"DataStoreService:GetDataStore('coins'):SetAsync(tostring(player.UserId), datas[player].coins)". You are setting datas[PLAYER].coins. Im pretty sure this is the issue. Try implimenting a auto save loop to get error outputs. Try datas[player.UserId]. Dont forget to change line 10 aswell.

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