Trying to find a way around this bug

im having trouble with a function i made in which the main job of the script i am having trouble with’s problem is to send information to a “Safe Teleport” script which requires both the placeId and the place to be TP’d to, The problem is i am putting the function which recieves the information from inside another function so it can pull the information from that function however this leads into the problem beucase everytime the parent function is called it creates a connection to the secondary function making it so if i press the parent function 5 times then click the children function regarless the child will run 5 times.

–Code


local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local Worlds = ReplicatedStorage:WaitForChild(“Worlds”)

local ElevatorPart =
workspace:WaitForChild(“Teleporter”):WaitForChild(“Elevator1”):WaitForChild(“Enterance”)
local movingEvent = ReplicatedStorage:WaitForChild(“MovingElevator”)
local elevatorEvent = ReplicatedStorage:WaitForChild(“Elevator”)
local gui = script.Parent
local SelectableWorlds = gui:WaitForChild(“MainElevator”):WaitForChild(“SelectWorld”)
local SelectedWorlds = gui:WaitForChild(“MainElevator”):WaitForChild(“WorldSelected”)
local WorldsAvailible = SelectableWorlds:WaitForChild(“Worlds”)
local Stages = SelectedWorlds:WaitForChild(“Stages”)
local StageName = SelectedWorlds:WaitForChild(“StageFrame”):WaitForChild(“StageInfoHolder”):WaitForChild(“TextLabel”)
local StageNickName = SelectedWorlds:WaitForChild(“StageFrame”):WaitForChild(“StageDetails”)
local StageMiniName = StageNickName:WaitForChild(“StageName”)
local StageRewards = SelectedWorlds:WaitForChild(“StageFrame”):WaitForChild(“StageRewards”):WaitForChild(“ScrollingFrame”)
local TeleportPlayers = ReplicatedStorage:WaitForChild(“TeleportPlayers”)
local PlayButton = StageName.Parent:WaitForChild(“ImageButton”)

local ToiletWorld = WorldsAvailible:WaitForChild(“Toilet”)
local ColorfulWorld = WorldsAvailible:WaitForChild(“Rainbow”)
local NightWatchWorld = WorldsAvailible:WaitForChild(“Five”)
local DoorsWorld = WorldsAvailible:WaitForChild(“Door”)

local Stage1 = Stages:WaitForChild(“Stage1”)
local Stage2 = Stages:WaitForChild(“Stage2”)
local Stage3 = Stages:WaitForChild(“Stage3”)
local Stage4 = Stages:WaitForChild(“Stage4”)
local Stage5 = Stages:WaitForChild(“Stage5”)

local exitBtn = gui.Exit
local camera = workspace.CurrentCamera

movingEvent.OnClientEvent:Connect(function()
exitBtn.Visible = false
end)

elevatorEvent.OnClientEvent:Connect(function(elevator)
exitBtn.Visible = true
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = elevator.Camera.CFrame
end)

exitBtn.Activated:Connect(function()
local player = game.Players.LocalPlayer
SelectableWorlds.Visible = false
SelectedWorlds.Visible = false
exitBtn.Visible = false
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = player.Character.Humanoid

elevatorEvent:FireServer()

end)

local function SelectedStage(World, Stage)
local StageOn = nil
print(Stage)
local ToiletWorld = 15856606795
if World == “Toilet World” then
StageOn = 15856606795
else
StageOn = 0
end
local StageSelected = Stage
local FoundStage = Worlds:FindFirstChild(World, true)
print(FoundStage)
print(StageSelected)
local StageNumberFound = FoundStage:FindFirstChild(StageSelected, true)
local BossImage = StageNumberFound:FindFirstChild(“BossImage”)
local StageName = StageNumberFound:FindFirstChild(“StageName”)
local CoinReward = StageNumberFound:FindFirstChild(“CoinReward”)
local GemReward = StageNumberFound:FindFirstChild(“GemReward”)

local Boss = StageNickName:WaitForChild("BossImage")
local Gems = StageRewards:WaitForChild("Gems").TextLabel
local Coins = StageRewards:WaitForChild("Coins").TextLabel

Gems.Text = GemReward.Value
Coins.Text = CoinReward.Value
Boss.Image = BossImage.Image
StageMiniName.Text = StageName.Value
PlayButton.Activated:Connect(function()
	local Players = game:GetService("Players")
	local player = Players.LocalPlayer
	print(StageOn)
	if StageOn == 0 then
	else
		TeleportPlayers:FireServer(StageOn)
	end
end)

end

local function WorldStage(Stage, Name)
local StageSelected = nil
local FoundStage = nil
local Num1 = false
local Num2 = false
local Num3 = false
local Num4 = false
local Num5 = false
ToiletWorld.ImageButton.Activated:Connect(function()
StageSelected = “Toilet World”
print(StageSelected)
FoundStage = Worlds:FindFirstChild(StageSelected, true)
StageName.Text = StageSelected
Num1 = true
end)
NightWatchWorld.ImageButton.Activated:Connect(function()
StageSelected = “Night Watch”
print(StageSelected)
FoundStage = Worlds:FindFirstChild(StageSelected, true)
StageName.Text = StageSelected
Num2 = true
end)
ColorfulWorld.ImageButton.Activated:Connect(function()
StageSelected = “Colorful Friends”
print(StageSelected)
FoundStage = Worlds:FindFirstChild(StageSelected, true)
StageName.Text = StageSelected
Num3 = true
end)
DoorsWorld.ImageButton.Activated:Connect(function()
StageSelected = “Doors”
print(StageSelected)
FoundStage = Worlds:FindFirstChild(StageSelected, true)
StageName.Text = StageSelected
Num4 = true
end)

Stage1.ImageButton.Activated:Connect(function()
	if Num1 == true or Num2 == true or Num3 == true or Num4 == true or Num5 == true then
		print(StageSelected)
		SelectedStage(StageSelected, "Stage1")
	end
end)
Stage2.ImageButton.Activated:Connect(function()
	if Num1 == true or Num2 == true or Num3 == true or Num4 == true or Num5 == true then
		print(StageSelected)
		SelectedStage(StageSelected, "Stage2")
	end
end)
Stage3.ImageButton.Activated:Connect(function()
	if Num1 == true or Num2 == true or Num3 == true or Num4 == true or Num5 == true then
		print(StageSelected)
		SelectedStage(StageSelected, "Stage3")
	end
end)
Stage4.ImageButton.Activated:Connect(function()
	if Num1 == true or Num2 == true or Num3 == true or Num4 == true or Num5 == true then
		print(StageSelected)
		SelectedStage(StageSelected, "Stage4")
	end
end)
Stage5.ImageButton.Activated:Connect(function()
	if Num1 == true or Num2 == true or Num3 == true or Num4 == true or Num5 == true then
		print(StageSelected)
		SelectedStage(StageSelected, "Stage5")
	end
end)

end

ToiletWorld.ImageButton.Activated:Connect(function()
gui:WaitForChild(“MainElevator”):WaitForChild(“WorldSelected”).Visible = true
end)
NightWatchWorld.ImageButton.Activated:Connect(function()
gui:WaitForChild(“MainElevator”):WaitForChild(“WorldSelected”).Visible = true
end)
ColorfulWorld.ImageButton.Activated:Connect(function()
gui:WaitForChild(“MainElevator”):WaitForChild(“WorldSelected”).Visible = true

end)
DoorsWorld.ImageButton.Activated:Connect(function()
gui:WaitForChild(“MainElevator”):WaitForChild(“WorldSelected”).Visible = true
end)

local function MakeVisible(Stage, StageNumber)
gui:WaitForChild(“MainElevator”):WaitForChild(“SelectWorld”).Visible = true
WorldStage()
end

– Assuming ‘part’ is the part you’re working with
local part = ElevatorPart
local cooldown = 1 – Cooldown in seconds
local canTouch = true – Variable to control touch availability

local function onTouch(otherPart)

local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)

if player and canTouch then
	MakeVisible()
	print(player.Name .. " touched the part.")

	-- Set canTouch to false and start the cooldown
	canTouch = false
	wait(cooldown)

	-- After the cooldown, allow the part to be touched again
	canTouch = true
end

end

– Connect the function to the part’s Touched event
part.Touched:Connect(onTouch)

The problem is in the SelectedStage function and the child would be the PlayButton.Activated:Connect

Whoever helps me solve this will be awarded 1,000 bonus.

You need to make sure you only connect the buttons once, like this:

local Num1, Num2, Num3, Num4, Num5 = false, false, false, false, false

local function SelectedStage(World, Stage)
    -- ur code here
end

local function WorldStage()
    local StageSelected = nil
    local FoundStage = nil

    ToiletWorld.ImageButton.Activated:Connect(function()
        StageSelected = "Toilet World"
        print(StageSelected)
        FoundStage = Worlds:FindFirstChild(StageSelected, true)
        StageName.Text = StageSelected
        Num1 = true
    end)

    NightWatchWorld.ImageButton.Activated:Connect(function()
        StageSelected = "Night Watch"
        print(StageSelected)
        FoundStage = Worlds:FindFirstChild(StageSelected, true)
        StageName.Text = StageSelected
        Num2 = true
    end)

    ColorfulWorld.ImageButton.Activated:Connect(function()
        StageSelected = "Colorful Friends"
        print(StageSelected)
        FoundStage = Worlds:FindFirstChild(StageSelected, true)
        StageName.Text = StageSelected
        Num3 = true
    end)

    DoorsWorld.ImageButton.Activated:Connect(function()
        StageSelected = "Doors"
        print(StageSelected)
        FoundStage = Worlds:FindFirstChild(StageSelected, true)
        StageName.Text = StageSelected
        Num4 = true
    end)

    local function ConnectStageButton(button, stageName)
        button.Activated:Connect(function()
            if Num1 or Num2 or Num3 or Num4 or Num5 then
                print(StageSelected)
                SelectedStage(StageSelected, stageName)
            end
        end)
    end

    ConnectStageButton(Stage1.ImageButton, "Stage1")
    ConnectStageButton(Stage2.ImageButton, "Stage2")
    ConnectStageButton(Stage3.ImageButton, "Stage3")
    ConnectStageButton(Stage4.ImageButton, "Stage4")
    ConnectStageButton(Stage5.ImageButton, "Stage5")
end

ToiletWorld.ImageButton.Activated:Connect(function()
    gui:WaitForChild("MainElevator"):WaitForChild("WorldSelected").Visible = true
    WorldStage()
end)

NightWatchWorld.ImageButton.Activated:Connect(function()
    gui:WaitForChild("MainElevator"):WaitForChild("WorldSelected").Visible = true
    WorldStage()
end)

ColorfulWorld.ImageButton.Activated:Connect(function()
    gui:WaitForChild("MainElevator"):WaitForChild("WorldSelected").Visible = true
    WorldStage()
end)

DoorsWorld.ImageButton.Activated:Connect(function()
    gui:WaitForChild("MainElevator"):WaitForChild("WorldSelected").Visible = true
    WorldStage()
end)