Function running even if the condition is false

I have this function that I call only once :

local function SetClass(screen)
	
	local stop = false
	
	for i, v in pairs(screen.Classes:GetChildren()) do
		if v:IsA("Frame") then
			v.MouseEnter:Connect(function()
				if not stop then
					print("Changed size of the class")
					local currentSize = v.Size.X.Scale
					local remainingSize = math.abs(0.275 - currentSize)
					local duration = (remainingSize / 0.025) / 4
					ChangeSize(v, UDim2.new(0.275, 0, 0.935, 0), duration, stop)
				end
			end)
		end
		
		v.InputBegan:Connect(function(input)
			if input.UserInputType == (Enum.UserInputType.MouseButton1 or Enum.UserInputType.Touch) and not stop then
				print(not stop)
				if passes[v.Name] then 
					if MarketPlaceService:PlayerOwnsAsset(player, passes[v.Name]) then
						ticketInfos.class = v.Name
						print("Changed class")
					else
						MarketPlaceService:PromptPurchase(player, passes[v.Name])
					end
				else
					ticketInfos.class = v.Name
				end	
				stop = true
				print("Stop variable has been set to true")
			end
		end)
	end	
	repeat wait() until stop
	print(stop)
	local currentPos = screen.Passeport.Position.X.Scale
	local remainingPos = math.abs(0 - currentPos)
	local duration = remainingPos / 1 / 2
	ShowGui(screen.Passeport, UDim2.new(0, 0, 0.5, 0), 5)
	TweenService:Create(screen.LoadingScreen.Chargement, TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, true), {TextTransparency = 1}):Play()
	wait(0.6)
	screen.LoadingScreen.Chargement.Text = "Veuillez scanner votre passeport"
end

But when the “stop’” variable is true, the MouseEnter and InputBegan can still continue running even with a if statement, here’s a video to understand, if I click really fast, it will change the class, and after the stop variable has been set to true, the MouseEnter function still run but doesn’t print “Changed size of the class” but changes size of the class, like it is dodging the print fuction

Any help will be appreciated, thanks.

i’m pretty sure this is because you :Connect, but never disconnect

essentially, connection gets created and it will stay connected until you tell it to disconnect

try adding 2 variables like connection1 and connection2, basically:

local con1 = nil
local con2 = nil

con1 = mouseenter...
con2 = inputbegan...

-- // Put these two at the end of the function
con1:Disconnect()
con2:Disconnect()

I tried, but it didn’t worked, I think it’s because of the loop, but normally, the stop variable should prevent the function from running, because the variable is global to the loop, but I still don’t know why it’s doing that.

well, i tried

gotta leave this to someone else, sorry

Don’t worry, thanks for trying.

As @nots7d pointed out, this is likely due to connections not disconnecting. The stop variable indeed prevents certain actions from occurring within the event handlers, but it does not prevent the handlers themselves from being executed.

I’ve added a table called connections to store all event connections.

  • Storing Connections: Each :Connect call is stored in the connections table.
  • Disconnecting Connections: Once stop is set to true, all stored connections are disconnected by iterating over the connections table.
local function SetClass(screen)
    local stop = false

    -- Store connections to disconnect them later
    local connections = {}

    for i, v in pairs(screen.Classes:GetChildren()) do
        if v:IsA("Frame") then
            table.insert(connections, v.MouseEnter:Connect(function()
                if not stop then
                    print("Changed size of the class")
                    local currentSize = v.Size.X.Scale
                    local remainingSize = math.abs(0.275 - currentSize)
                    local duration = (remainingSize / 0.025) / 4
                    ChangeSize(v, UDim2.new(0.275, 0, 0.935, 0), duration, stop)
                end
            end))
        end

        table.insert(connections, v.InputBegan:Connect(function(input)
            if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
                if not stop then
                    print(not stop)
                    if passes[v.Name] then
                        if MarketPlaceService:PlayerOwnsAsset(player, passes[v.Name]) then
                            ticketInfos.class = v.Name
                            print("Changed class")
                        else
                            MarketPlaceService:PromptPurchase(player, passes[v.Name])
                        end
                    else
                        ticketInfos.class = v.Name
                    end
                    stop = true
                    print("Stop variable has been set to true")

                    -- Disconnect all connections when stop is set to true
                    for _, connection in pairs(connections) do
                        connection:Disconnect()
                    end
                end
            end
        end))
    end

    repeat wait() until stop
    print(stop)
    local currentPos = screen.Passeport.Position.X.Scale
    local remainingPos = math.abs(0 - currentPos)
    local duration = remainingPos / 1 / 2
    ShowGui(screen.Passeport, UDim2.new(0, 0, 0.5, 0), 5)
    TweenService:Create(screen.LoadingScreen.Chargement, TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, true), {TextTransparency = 1}):Play()
    wait(0.6)
    screen.LoadingScreen.Chargement.Text = "Veuillez scanner votre passeport"
end

Please let me know if this approach works.

No, it does the exact same thing showed in the video.

So, after many help from the bot of the Roblox Studio Discord server, I finally understood why it does that, it is because of the fact that when you use :Connect() , it doesn’t run instanly after being called, here’s the bot message :

This delay you are experiencing is likely due to the way Roblox processes events. When you connect a function to an event using :Connect(), it doesn’t execute immediately. Instead, it gets added to a queue of functions waiting to be executed.

If you need to ensure that the variable is updated instantly after changing it, you can consider restructuring your code to directly call a function that updates the variable instead of relying on event connections.

If you provide more details or code snippets, I can offer more specific advice on how to address this issue.

And that fixed the issue, I just made new normal functions, thanks for the help you all provided.

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