Issue with saving player currency

I have a currency script that should allow players to retain their money after gaining it. but whenever you leave the game and rejoin it doesn’t save the money gained while playing. not really sure what is going wrong here.

--CURRENCY SYSTEM--

local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService") :GetDataStore("AdventureClickerStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
		print("Player Loaded")
	else
		currency.Value = 100
		print("New Player")
	end
	
	
end)





local DataStore = game:GetService("DataStoreService") :GetDataStore("Weapons")

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

	local folder = Instance.new("Folder")
	folder.Name = "Weapons"
	folder.Parent = player

	local ID = player.UserId
	local savedData = nil

	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	print("loaded weapons")

end)



--Saving when Player Leaves
game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
print("logged out successful")

--when game is ready to shut down
game:BindToClose(function()
	
	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game shut down")
		end
	end
	
	task.wait(3)
	
end)


--PLAYER DAMAGE--

game.Players.PlayerAdded:Connect(function(player)
	
	local damage = Instance.new("IntValue")
	damage.Name = "damage"
	damage.Parent = player
	damage.Value = 1
	
end)

Thanks for the help!

1 Like
game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

Just only one value?

what do you mean by just only one value?

also should that be ID.player.leaderstats… or is the " , " correct?

Maybe it has to do with the fact, that you are trying to use this variable with two different definitions.

oh yeah, it seems trying to have another data store messed things up. simply removing that worked well.

1 Like

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