Why is my `AlternativeCloseTabs()` function running 24 times?


Explanation

My goal is to simply have the frame open when I hit the keybind which is determined by a Value already set.


Issue

My issue is that my AlternativeCloseTabs() function runs approximately 24 times without being in a loop.
image


Solutions

I’ve tried the following;

  • Reprogramming everything from scratch x2;
  • Removing variables;
  • Adding & removing certain if statements;

Script Info

This is all in a local script.


Issue Code

local function AlternativeCloseTabs(Name)
	for _,GUI in pairs (script.Parent.Parent.Parent.GUIs:GetChildren()) do
		for c,SFrame in pairs (script.Parent.GUIScrollingFrame:GetChildren()) do
			for x,Button in pairs (SFrame:GetChildren()) do
				if GUI.Name ~= Name then
					GUI.Visible = false
				end
				if Button.Indicator.BackgroundColor3 ~= Color3.fromRGB(0,0,0) then
					Button.Indicator.BackgroundColor3 = Color3.fromRGB(170,0,0) 
				end
				if Button.Text ~= Button.LabelName.Value then
					Button.Text = Button.LabelName.Value..": Closed"	
				end
				print("ClosedTabs")
			end
		end
	end	
end

Full Code

--//Made By MillerrIAm\\--
--/Variables\--
local UIS = game:GetService("UserInputService")
local Tween = false
--/Functions\--
local function TweenCloseTabs(Name)
	for _,GUI in pairs (script.Parent.Parent.Parent.GUIs:GetChildren()) do
		for c,SFrame in pairs (script.Parent.GUIScrollingFrame:GetChildren()) do
			for x,Button in pairs (SFrame:GetChildren()) do
				if GUI.Name ~= Name then
					GUI:TweenPosition(UDim2.new(-1, 0, GUI.Position.Y.Scale, 0), 'In', 'Linear', 0.3)
				end
				if Button.Indicator.BackgroundColor3 ~= Color3.fromRGB(0,0,0) then
					Button.Indicator.BackgroundColor3 = Color3.fromRGB(170,0,0) 
				end
				if Button.Text ~= Button.LabelName.Value then
					Button.Text = Button.LabelName.Value..": Closed"	
				end
			end
		end
	end	
end

local function TweenTabControl(GUIName)
	local Panel = script.Parent.Parent.Parent["Settings Frame"]
	local GUIs = script.Parent.Parent.Parent.GUIs
	for c,Frame in pairs (script.Parent.GUIScrollingFrame:GetChildren()) do
		for _,Button in pairs (Frame:GetChildren()) do
			if Panel.Position == UDim2.new(0.001, 0, Panel.Position.Y.Scale, 0) then
				if GUIs[GUIName].Position == UDim2.new(-1, 0, GUIs[GUIName].Position.Y.Scale, 0) then	
					GUIs[GUIName]:TweenPosition(UDim2.new(0.1, 0, GUIs[GUIName].Position.Y.Scale, 0), 'In', 'Linear', 0.3)
					if Frame.Name == GUIName.." Frame" then
						Button.Indicator.BackgroundColor3 = Color3.fromRGB(0,170,0)
						Button.Text = Button.LabelName.Value..": Open"
					end
				else
					GUIs[GUIName]:TweenPosition(UDim2.new(-1, 0, GUIs[GUIName].Position.Y.Scale, 0), 'In', 'Linear', 0.3)
					if Frame.Name == GUIName.." Frame" then
						Button.Indicator.BackgroundColor3 = Color3.fromRGB(170,0,0)
						Button.Text = Button.LabelName.Value..": Closed"
					end
				end
			end
		end
	end
end

local function AlternativeCloseTabs(Name)
	for _,GUI in pairs (script.Parent.Parent.Parent.GUIs:GetChildren()) do
		for c,SFrame in pairs (script.Parent.GUIScrollingFrame:GetChildren()) do
			for x,Button in pairs (SFrame:GetChildren()) do
				if GUI.Visible == true then
					GUI.Visible = false
				end
				if Button.Indicator.BackgroundColor3 ~= Color3.fromRGB(0,0,0) then
					Button.Indicator.BackgroundColor3 = Color3.fromRGB(170,0,0) 
				end
				if Button.Text ~= Button.LabelName.Value then
					Button.Text = Button.LabelName.Value..": Closed"	
				end
			end
		end
	end	
end

local function AlternativeTabControl(GUIName)
	local Panel = script.Parent.Parent.Parent["Settings Frame"]
	local GUIs = script.Parent.Parent.Parent.GUIs
	for c,Frame in pairs (script.Parent.GUIScrollingFrame:GetChildren()) do
		for _,Button in pairs (Frame:GetChildren()) do
			if Panel.Position == UDim2.new(0.001, 0, Panel.Position.Y.Scale, 0) then
				if GUIs[GUIName].Visible == false then
					GUIs[GUIName].Visible = true
					if Frame.Name == GUIName.." Frame" then
						Button.Indicator.BackgroundColor3 = Color3.fromRGB(0,170,0)
						Button.Text = Button.LabelName.Value..": Open"
					end
				elseif GUIs[GUIName].Visible == true then
					GUIs[GUIName].Visible = false
					if Frame.Name == GUIName.." Frame" then
						Button.Indicator.BackgroundColor3 = Color3.fromRGB(170,0,0)
						Button.Text = Button.LabelName.Value..": Closed"
					end
				end
			end
		end
	end
end

local function Check()
	local Panel = script.Parent.Parent.Parent["Settings Frame"]
	local GUIs = script.Parent.Parent.Parent.GUIs
	for _,GUI in pairs (GUIs:GetChildren()) do
		if Tween then
			GUI.Position = UDim2.new(-1, 0, Panel.Position.Y.Scale, 0)
			GUI.Visible = true
		else
			GUI.Visible = false
			GUI.Position = UDim2.new(0.1, 0, Panel.Position.Y.Scale, 0)
		end
		print("Check Complete")
	end
end

--/Main Script\--
Check()
UIS.InputBegan:Connect(function(KeyPressed, gp)
	local GUIs = script.Parent.Parent.Parent.GUIs
	local Key = Enum.KeyCode
	for _,GUI in pairs (GUIs:GetChildren()) do
		if KeyPressed.KeyCode == Key[GUI.Keybind.Value] then
			if UIS:GetFocusedTextBox() == nil then
				if Tween then
					TweenCloseTabs(GUI.Name)
					TweenTabControl(GUI.Name)
				else
					AlternativeCloseTabs(GUI.Name)
					AlternativeTabControl(GUI.Name)
				end
			end
		end
	end
end)

Thank you for any help you can give me.

1 Like

Is the Gui Enabled?

30charssss

1 Like

This might be totally useless, as I literally just looked this up on the Dev Reference Pages and I’m new to Lua / Roblox Scripting.
Maybe you are mismatching Bytes and Strings? Is GUI.Keybind.Value a letter? It might be failing to find it in the Enum because when I look at the KeyCode reference here, it seems like the values are all Bytes or Integers.

1 Like

Yes it is, here’s a photo to describe how the U.Is are set-up.

1 Like

The Keybind function works perfectly fine, I’ve tested that function countless times. It works when I use the TweenService however it doesn’t work when I just attempt to have the U.I become visible.

There is your issue, you are using regular scripts instead of local scripts on the character

1 Like

Incorrect, I’m using a localscript for the KeybindSystem.
image

Yes, very much, as server scripts can’t do anything to a client’s GUI

1 Like

Any Scrips done on the client need to be local or they wont run. Try printing on the first line

Exactly what is going on with all those other scripts that are above KeybindScript? It seems unrelated, but I’d like to know.

1 Like

No, the only script that does anything on the client side is the Keybind Script. The other scripts are for my ScrollingFrames.
Something entirely different.

I.E; The Script for my frames.

--Made By MillerrIAm
--------------Variables------------
local ItemName = script.ItemName.Value
local Items = script.Parent.Parent.Parent[ItemName]:GetChildren()
local SFName = script.SFrameName.Value
local SFrame = script.Parent[SFName]
local Value = 0
local Number = 0
-------------Main Script------------
while script.Disabled == false do
	wait(0.05)
	for i,v in pairs(Items) do
		if Value == 0 then
			local frame = SFrame.Frame:Clone()
			frame.Visible = true
			frame.Position = frame.Position + UDim2.new(0,0,Number,0)
			frame.Name = v.Name.." Frame"
			frame.Label.LabelName.Value = v.Name
			frame.Label.Text = v.Name
			frame.Label.Name = v.Name
			frame.Parent = SFrame
			Number = Number + 0.005
		end
	end
	script.Disabled = true
end

That’s not entirely true, all my scripts that are used to do something on the Server Side are done using Local Scripts and Remote Events. Any Server Script used is for something the Client has to see that’s on the Server Side only.

1 Like

Why do I see a bunch of commands to move GUI objects? That’s a Script, right?
Also, how come you aren’t tweening? It seems like it is moving a GUI object(s possibly) around the screen, but inefficiently by stepping up a value in a for loop. Is there any purpose behind that? Or am I just completely missing something?
I know we are getting off of the original question, but I just kinda want to know what’s happening there.

I’ll break everything down for you.


The Server Scripts

As you can see in the screenshot below, the server scripts are used for the ScrollingFrames so they can make the buttons I need them to make.

BEFORE ENTERING THE GAME

image

AFTER ENTERING THE GAME

image


The Local Script

As you can see in the photo below, it’s the script that controls anything regarding the keybind system.


Keybind Trigger

This is what triggers the Keybinds to work and it works perfectly fine as they work when I’m using the TweenFunctions I created which means the error is in my Error Part which I posted in the original post.

--/Main Script\--
Check()
UIS.InputBegan:Connect(function(KeyPressed, gp)
	local GUIs = script.Parent.Parent.Parent.GUIs
	local Key = Enum.KeyCode
	for _,GUI in pairs (GUIs:GetChildren()) do
		if KeyPressed.KeyCode == Key[GUI.Keybind.Value] then
			if UIS:GetFocusedTextBox() == nil then
				if Tween then
					TweenCloseTabs(GUI.Name)
					TweenTabControl(GUI.Name)
				else
					AlternativeCloseTabs(GUI.Name)
					AlternativeTabControl(GUI.Name)
				end
			end
		end
	end
end)

There’s proof above that the ServerScripts are doing their job in the AFTER ENTERING THE GAME photo.
I hope this clears up the confusion for you. @Wizard101fire90 & @BritishAPT

1 Like

I’m giving this a bump because of my latest discovery, I hope that someone may be able to help me with this issue.

UIS.InputBegan:Connect(function(KeyPressed, gp)
    if gp then return end
	local GUIs = script.Parent.Parent.Parent.GUIs
	local Key = Enum.KeyCode
	for _,GUI in pairs (GUIs:GetChildren()) do
		if KeyPressed.KeyCode == Key[GUI.Keybind.Value] then
			if UIS:GetFocusedTextBox() == nil then
				if Tween then
					TweenCloseTabs(GUI.Name)
					TweenTabControl(GUI.Name)
				else
					AlternativeCloseTabs(GUI.Name)
					AlternativeTabControl(GUI.Name)
				end
			end
		end
	end
end)

You want to check if the game already received the input, so without the ‘if gp then return end’ check it will run multiple times

1 Like

Your print statement is nested inside three for-loops. Try moving it to the very end of the function to make sure the function itself is only being called oncce.

1 Like