Repeatedly Cloning GUI

  1. I’m making a simple script for when a player walks through a door (collides with a part) It loads a BlackLoadingScreen GUI and teleports the player to the end location, then loads the FactoryGUI

  2. Issue is that whenever the player goes through the door, it repeatedly clones the GUI a bunch of times.

  3. I only want it to clone once, I’ve tried fixing it but It’s just not seeming to work.

SCRIPT LOCATED IN StarterGui.BlackFadingScreen.Frame

-- Script for teleporting a player to another part's position upon collision
local teleportPart = game.Workspace.touchedPart
local defaultFactory = game:GetService("Workspace").DefaultFactory  -- Assuming this script is a child of the teleporting part
local destinationPart = defaultFactory.Utils.endPart
local Players = game:GetService("Players") -- Replace with the name of the destination part
local RepStorage = game:GetService("ReplicatedStorage")

local function onTouched(other)
	local character = other.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	local player = Players:GetPlayerFromCharacter(character)
	local facGUIloaded = false

	-- If touched by a player's humanoid, teleport the player to the destination part
	if humanoid then
		local facGUI = RepStorage.FactoryGUI:Clone()
		local gui = script.Parent
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
		local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {BackgroundTransparency = 0})
		gui.Visible = true
		tween:Play()
		wait(2)
		character.HumanoidRootPart.CFrame = destinationPart.CFrame
		facGUI.Parent = player.PlayerGui
		gui.Parent.Parent.VHs.Enabled= true
		wait(0.25)
		local tweenInfo2 = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
		local tween2 = game:GetService("TweenService"):Create(gui, tweenInfo2, {BackgroundTransparency = 1})
		tween2:Play()
		wait(1.5)
		gui.Visible = false	
	end
end

-- Connect the 'Touched' event of the teleport part to the 'onTouched' function
teleportPart.Touched:Connect(onTouched)

Have you tried deleting the clone once you have finished with it?

1 Like

Try using a debouse so the touch even only happens once. What might be happening is your player is touching the part multiple times causing the code to run multiple times not sure if this helps.

1 Like

Yeah that would also maybe work if that is the issue.

E.g. debounce:

local debounce = false;

--// on click
if not debounce then

debounce = true
task.spawn(function()
task.wait(0.5);
debounce = false;
end);
end;
``` (dont use this code it is just an example.)
1 Like

You just wanna use debounces, which are Booleans that can impose a restriction on how many times something can run. Here’s an example

local deb = false
script.Parent.Touched:Connect(function()
   if hit.Parent:FindFirstChild(“Humanoid”) then
      if deb then return end — Stop the function from proceeding
      deb = true
      print(“hi”)
      task.wait(2)
      deb = false
   end
end)

This will make sure that hi only prints once when the player walks over the part briefly
You can implement this into your script to prevent the gui from cloning so many times

1 Like

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