:Disconnect() doesn't work to disable function

That’s not how you are supposed to use :Disconnect() here is a working example:

local RunService = game:GetService("RunService")

local fired = 0 --amount of times the event fired

--you have to set the variable before referencing the signal
local connection 
connection = RunService.RenderStepped:Connect(function()
	fired += 1
	print(fired)
	--after it runs 1000 times
	if fired > 1000 then 
		--disconnect the event
		connection:Disconnect()
	end
end)
1 Like

You could maybe setup a debounce Boolean of some sort, and once the code runs, set the debounce to true until you need it to run again (If you do). You could do a while loop and whenever a certain thing is equal it runs, then when its done, set it to a value that will make sure the loop doesn’t run again.

local plrs = game:GetService("Players")
local mps = game:GetService("MarketplaceService")

local function givewinfunction()
	for _, plr in ipairs(plrs:GetPlayers()) do
		if plr then
			local leaderstats = plr.leaderstats
			if leaderstats then
				local wins = leaderstats.Wins
				local coins = leaderstats.Coins
				if wins and coins then
					if mps:UserOwnsGamepassAsync(plr.UserId, 25789902) then
						wins.Value += 1
						coins.Value += 20
					else
						wins.Value += 1
						coins.Value += 10
					end
				end
			end
		end
	end
end

You don’t need to disconnect anything here since the function is only ran when it is called to do so, I’ve combined everything into a single function and added some minor improvements, I didn’t take a look at the module method as a lot of it was underlined blue (indicating that it was missing declarations/references).

+=, -=, *= and /= are all valid Luau syntax.

2 Likes

I got no errors in output but it doesn’t work still…

Guilty, I forgot Luau was a thing :sweat_smile:

1 Like