Function keeps the same string I used/cancelled earlier before I run it again with a different string

1. What do I want to achieve?:

I am making a function that increases a player’s stat multiplier by clicking a button, you’ll see that I didn’t know how to make this more optimized so I made it like this:

local function IncreaseMultiplier(stat: "Strength" | "Agility" | "Power")
	
	print(stat)
	confirm.Visible = true
	frame.Visible = false
	confirm:TweenSize(UDim2.new(0.25,0,0.25,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 1)

	confirm.No.Activated:Connect(function()
		confirm.Visible = false
		confirm:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0)
		frame.Visible = true
	end)

	confirm.Yes.Activated:Connect(function()
		confirm.Visible = false
		confirm:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0)
		frame.Visible = true

		if stat == "Strength" then
			print(stat)
			purchaseMlt:FireServer("SthMultiplier")
		elseif stat == "Agility" then
			print(stat)
			purchaseMlt:FireServer("AgyMultiplier")
		elseif stat == "Power" then
			print(stat)
			purchaseMlt:FireServer("PwrMultiplier")
		end	
	end)
end

statsFrame.Strength.Multiplier.Activated:Connect(function()
	IncreaseMultiplier("Strength")
end)

statsFrame.Agility.Multiplier.Activated:Connect(function()
	IncreaseMultiplier("Agility")
end)

statsFrame.Power.Multiplier.Activated:Connect(function()
	IncreaseMultiplier("Power")
end)

In ModuleScript:

function manager.GetMultiplier(player: Player, multiplier: string)
	local profile = manager.Profiles[player]
	if not profile then return end
	profile.Data[multiplier] *= 2
	player.Multipliers[multiplier].Value = profile.Data[multiplier]
	updateMultiplier:FireClient(player)
end

purchaseEvent.OnServerEvent:Connect(manager.GetMultiplier)

2. What is the issue?

As you are seeing, I added prints to know what’s going on with the script and in the statements part, they are supposed to read the stat string and use that string to FireServer the purchase event and then the manager.GetMultiplier gets care of reading and finding that string in player’s data and double the multiplier.

However, the issue is that whenever I cancel the confirmation by clicking confirm.No button and then when I use it for other stat instead, the string of earlier somehow saves and I end multiplying two stats.

3. What solutions have I tried so far?

I tried to add task.spawn for the function to run once, didn’t work. I was going to try corountine.yield but from what I read, it doesn’t seem to help. I don’t recognize what is causing this mistake so is there somebody that can explain what am I doing wrong? Thanks

It’s because you’re not properly managing your event listeners; you’re creating new ones each time IncreaseMultiplier is called, without clearing the old ones. There are any number of ways to fix this.

local function IncreaseMultiplier(stat: "Strength" | "Agility" | "Power")

	local noEvent, yesEvent

	local function close()
		noEvent:Disconnect()
		yesEvent:Disconnect()
		confirm.Visible = false
		confirm:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0)
		frame.Visible = true
	end

	-- ...

	noEvent = confirm.No.Activated:Connect(close)

	yesEvent = confirm.Yes.Activated:Connect(function()
		close()
		-- ...
	end)
end

Woah that was impressive, I didn’t know this trick with functions. I’ll make sure to use this in case of this situation repeats in the future. Thanks!

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