-- This script only needs to run if increment values actually exist.

if earnTime > 0 and earnAmount ~= 0 then
	-- The first shop in the game that loads will parent this script to
	-- ServerScriptService. After that, the other shops will just change
	-- the value of playerObject, and the player gets added to the increment list.
	script.playerObject.Changed:connect(function(value)
		if value and value:IsA("Player") then
			for _, player in next, playersToIncrement do
				if player == value then return end
			end
			table.insert(playersToIncrement, value)
		end
		script.playerObject.Value = nil
	end)

	while wait(earnTime) and wait() do
		for i, player in next, playersToIncrement do
			if player then
				local cash = player.leaderstats:FindFirstChild(cashName)
				if cash then
					cash.Value = cash.Value + earnAmount
				end
			else
				table.remove(playersToIncrement, i)
			end
		end
	end
else
	script:Destroy()
end
