How to change "i" value in a for loop

I have a round system based on a for loop. I made a dev product to increase the number of “rounds” in a game incase someone needs extra time to do tasks, etc. I thought it would be as simple as when someone buys the dev products to increase an IntValue, but after a round, it resets to the round It would have been prior to buying the dev product. How would I fix this?

Simplified round script:

local RoundsUntilReset = game.ReplicatedStorage.RoundsUntilReset

while true do
	for i = RoundsUntilReset.Value, 0, -1 do
		RoundsUntilReset.Value = i
		wait(10)
	end
	print("game ended")
end

Dev Product Script:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")

-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}

-- ProductId 123123123 for +3 Rounds
productFunctions[123123123] = function(_receipt, player)
	local ReplicatedStorage = game.ReplicatedStorage
	local RoundsUntilReset = ReplicatedStorage.RoundsUntilReset
	if RoundsUntilReset then
		RoundsUntilReset.Value = RoundsUntilReset.Value + 3
		-- Indicate a successful purchase
		return true
	end
end




-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
	-- Determine if the product was already granted by checking the data store
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, result, errorMessage

	success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)
	-- If purchase was recorded, the product was already granted
	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end

	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId

	local success, isPurchaseRecorded = pcall(function()
		return purchaseHistoryStore:UpdateAsync(playerProductKey, function(alreadyPurchased)
			if alreadyPurchased then
				return true
			end

			-- Find the player who made the purchase in the server
			local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
			if not player then
				-- The player probably left the game
				-- If they come back, the callback will be called again
				return nil
			end

			local handler = productFunctions[receiptInfo.ProductId]

			local success, result = pcall(handler, receiptInfo, player)
			-- If granting the product failed, do NOT record the purchase in datastores.
			if not success or not result then
				error("Failed to process a product purchase for ProductId: " .. tostring(receiptInfo.ProductId) .. " Player: " .. tostring(player) .. " Error: " .. tostring(result))
				return nil
			end

			-- Record the transcation in purchaseHistoryStore.
			return true
		end)
	end)

	if not success then
		error("Failed to process receipt due to data store error.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	elseif isPurchaseRecorded == nil then
		-- Didn't update the value in data store.
		return Enum.ProductPurchaseDecision.NotProcessedYet
	else	
		-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt

Thanks for any help in advance :slight_smile:

local RoundsUntilReset = game.ReplicatedStorage.RoundsUntilReset

while true do
	if RoundsUntilReset.Value <= 0 then
		print("game ended")
		break --- Will break the loop, you can change it to continue if loop should never end
	end
	
	task.wait(1) --- Change the wait time to any number you want
	
	RoundsUntilReset.Value -= 1
end

You can make it like that instead

1 Like

Directly changing the i in a for loop is considered a bad practice in many languages and in Lua it’s not possible. Instead, you should use a while loop for this as shown above.

1 Like

I understand, just tried it and works perfectly!

Sorry for the late response, Marked as a Solution. Thanks :slight_smile:

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