UI script not running

Any idea why my UI script is not working? I’ve tried with CharacterAutoLoads on and off. “finished” prints but none of the button events work, and the LoadTeams script doesn’t work either. The UI is in StarterGui, I’ve tried it in ReplicatedFirst and it doesn’t work there either. No errors in output.

local Player = game:GetService("Players").LocalPlayer
local Menu = script.Parent.Frame
local TweenService = game:GetService("TweenService")

local function LoadTeams()
	task.spawn(function ()
		for _, Team in pairs(game:WaitForChild("Teams"):GetChildren()) do -- For each team...
			-- Create button
			local Button = script.TeamButton:Clone()
			Button.Name = Team.Name
			Button.Text = Team.Name:upper()
			Button.LayoutOrder = Team.LayoutOrder.Value

			-- Determine if it's available
			local Available = false
			if Team:FindFirstChild("GroupID") then
				if Player:IsInGroup(Team.GroupID.Value) then
					Available = true
				elseif Player:GetRankInGroup(9898752) >= 100 then
					--Available = true
				else
					Available = false
				end
			elseif Team == game.Teams["Basic Training"] then
				if Player:GetRankInGroup(9898752) == Team.MinRank.Value then
					Available = true
				else
					Available = false
				end
			elseif Team:FindFirstChild("MinRank") then
				if Player:GetRankInGroup(9898752) >= Team.MinRank.Value then
					Available = true
				else
					Available = false
				end
			else
				Available = true
			end

			-- Create default team if the player doesn't choose before clicking Deploy
			if Team ~= game.Teams["USSF Personnel"] and Team ~= game.Teams["Civilian"] then
				if Available and not script.SelectedTeam.Value then
					script.SelectedTeam.Value = Team
				end
			end

		--[[ Set logo if available
		if Team:FindFirstChild("GroupID") then
			Button.Logo.Image = game:GetService("GroupService"):GetGroupInfoAsync(Team:FindFirstChild("GroupID").Value).EmblemUrl
			Button.Logo.ImageTransparency = 0
		end]]

			-- Set the click-ability of the button
			if Available then
				Button.Interactable = true
				Button.BackgroundColor3 = Color3.fromRGB(40, 40, 40)
				Button.TextColor3 = Color3.fromRGB(255, 255, 255)
				Button.MouseEnter:Connect(function ()
					if not Button.Chosen.Value then
						game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(48, 48, 48)}):Play()
					end
				end)
				Button.MouseMoved:Connect(function ()
					if Button.BackgroundColor3 ~= Color3.fromRGB(48, 48, 48) and not Button.Chosen.Value then
						game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(48, 48, 48)}):Play()
					end
				end)
				Button.MouseLeave:Connect(function ()
					if not Button.Chosen.Value then
						game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(40, 40, 40)}):Play()
					end
				end)
				Button.MouseButton1Click:Connect(function ()
					if not Button.Interactable then return end
					for _, _Button in pairs(Menu.Main.TeamSelect:GetChildren()) do
						if _Button:IsA("TextButton") and _Button.Chosen.Value then
							_Button.Chosen.Value = false
							game.TweenService:Create(_Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(40, 40, 40)}):Play()
							game.TweenService:Create(_Button.UIPadding, TweenInfo.new(0.1), {PaddingTop = UDim.new(0.25, 0), PaddingBottom = UDim.new(0.25, 0)}):Play()
						end
					end
					Button.Chosen.Value = not Button.Chosen.Value
					if Button.Chosen.Value then
						game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(55, 55, 55)}):Play()
						game.TweenService:Create(Button.UIPadding, TweenInfo.new(0.1), {PaddingTop = UDim.new(0.225, 0), PaddingBottom = UDim.new(0.225, 0)}):Play()
					else
						game.TweenService:Create(Button.UIPadding, TweenInfo.new(0.1), {PaddingTop = UDim.new(0.25, 0), PaddingBottom = UDim.new(0.25, 0)}):Play()
					end
					script.SelectedTeam.Value = game.Teams[Button.Name]
				end)
			else
				Button.Interactable = false
				Button.BackgroundColor3 = Color3.fromRGB(20, 20, 20)
				Button.TextColor3 = Color3.fromRGB(130, 130, 130)
			end

			Button.Parent = Menu.Main.TeamSelect
		end
	end)
end

Menu.Main.Buttons.Deploy.MouseEnter:Connect(function ()
	TweenService:Create(Menu.Main.Buttons.Deploy, TweenInfo.new(0.1), {Size = UDim2.new(0.8, -2, 0.15, -2), BackgroundColor3 = Color3.fromRGB(255, 255, 255), TextColor3 = Color3.fromRGB(10, 10, 10)}):Play()
end)

Menu.Main.Buttons.Deploy.MouseLeave:Connect(function ()
	TweenService:Create(Menu.Main.Buttons.Deploy, TweenInfo.new(0.1), {Size = UDim2.new(0.8, 0, 0.15, 0), BackgroundColor3 = Color3.fromRGB(10, 10, 10), TextColor3 = Color3.fromRGB(220, 220, 220)}):Play()
end)

Menu.Main.Buttons.Deploy.MouseButton1Click:Connect(function ()
	if not Player.Character or Player.Character.Humanoid.Health <= 0 or Player.Team ~= script.SelectedTeam.Value then
		game.ReplicatedStorage.Deploy:FireServer(script.SelectedTeam.Value)
	end
	local Tween1 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(0.5), {BackgroundTransparency = 0})
	Tween1:Play()
	Tween1.Completed:Wait()
	Menu.Visible = false
	local Tween2 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(0.5), {BackgroundTransparency = 1})
	Tween2:Play()
	Tween2.Completed:Wait()
	script.Parent.Enabled = false
end)

game.ReplicatedStorage:WaitForChild("Died").OnClientEvent:Connect(function ()
	script.Parent.Enabled = true
	script.Parent.Overlay.BackgroundTransparency = 1
	Menu.Visible = false
	local Tween1 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(5), {BackgroundTransparency = 0})
	Tween1:Play()
	Tween1.Completed:Wait()
	game.ReplicatedStorage.LoadCharacter:FireServer()
	local Tween2 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(0.5), {BackgroundTransparency = 1})
	Tween2:Play()
	Tween2.Completed:Wait()
	script.Parent.Enabled = false
	Menu.Visible = true
end)

LoadTeams()
print("finished")
--script.Parent.Parent = Player.PlayerGui```
1 Like

I don’t think you can tamper with teams in LocalScript, imagine if exploiters could assign themselves to any team they want. Fire a remote event on button activation to change team.

Team is assigned through a server script
The issue is none of the UI code is running

2 Likes

Sorry, didn’t notice that. Maybe the frames are blocking the buttons from being pressed? Does button.Hover event work?

This is what it looks like, just the basic Roblox UI functions

Also could be noted that when I put print("hover") it doesn’t show in output

Welp, seems like they really do block the buttons try disabling .Active property of frames that could potentially do that

May I ask what class is the ‘SelectedTeam’ object?

Maybe this does something wrong? Try commenting anything that can disable button being interactable/active

edit: nvm if it wasnt interactable it wouldnt change colors

1 Like

To be honest your code is quite confusing, in your place I’d rewrite it without seemingly unnecessary stuff like defining a function which you are gonna call once, enclosing its conents in task.spawn??, etc

1 Like

the issue was that ur script tried to use scriptSelectedTeamValue but that object didnt exist so when it reached scriptSelectedTeamValue = Team it crashed silently inside taskspawn and stopped the rest of the buttons from loading or working

u gotta make sure there’s an ObjectValue named SelectedTeam inside the script before calling LoadTeams or else it just breaks without saying anything in output

U should add this before LoadTeams

if not script:FindFirstChild("SelectedTeam") then
	local val = Instance.new("ObjectValue")
	val.Name = "SelectedTeam"
	val.Parent = script
end
1 Like

is the button’s code a local script?

task.spawn shouldnt hide errors, thats what pcall does, and even then it doesnt halt the rest of the script

2 Likes

@LocalKarura

Yes it’s a LocalScript

@oplkel

It’s an ObjectValue

@ForeverSp4rK

It does already exist

Is there anything else that could have caused this? Maybe some sort of setting? This script used to work, but stopped all of a sudden. Not sure what change caused it

Yeah, the task.spawn part was just me messing around trying to find the issue. But the reason I put it into a function is so that later on I can script possibly a loading screen and other “refresh” functions if the player unlocks more teams while in-game

Wait the script is client side?

1 Like

Hey can u use debug like print(1) in the load team function and down and check where is stop printing

In the for loop ur looping through the game ur waitforchild instead use GetService

Ensure team button have child bool value have name Choosen and interactable

Ensure that the paths are correct like main.smth.smth

1 Like
print "1"

local Player = game:GetService("Players").LocalPlayer

script.Parent.Parent = Player:WaitForChild("PlayerGui")

local Menu = script.Parent.Frame
local TweenService = game:GetService("TweenService")

print "2 - variables loaded"

local function LoadTeams()
	print "3 - LoadTeams called"

	for _, Team in pairs(game:GetService("Teams"):GetChildren()) do -- For each team...
		print(Team.Name)
		-- Create button
		local Button = script.TeamButton:Clone()
		Button.Name = Team.Name
		Button.Text = Team.Name:upper()
		Button.LayoutOrder = Team.LayoutOrder.Value

		-- Determine if it's available
		local Available = false
		if Team:FindFirstChild("GroupID") then
			if Player:IsInGroup(Team.GroupID.Value) then
				Available = true
			elseif Player:GetRankInGroup(9898752) >= 100 then
				--Available = true
			else
				Available = false
			end
		elseif Team == game.Teams["Basic Training"] then
			if Player:GetRankInGroup(9898752) == Team.MinRank.Value then
				Available = true
			else
				Available = false
			end
		elseif Team:FindFirstChild("MinRank") then
			if Player:GetRankInGroup(9898752) >= Team.MinRank.Value then
				Available = true
			else
				Available = false
			end
		else
			Available = true
		end

		-- Create default team if the player doesn't choose before clicking Deploy
		if Team ~= game.Teams["USSF Personnel"] and Team ~= game.Teams["Civilian"] then
			if Available and not script.SelectedTeam.Value then
				script.SelectedTeam.Value = Team
			end
		end

		-- Set the click-ability of the button
		if Available then
			Button.Interactable = true
			Button.BackgroundColor3 = Color3.fromRGB(40, 40, 40)
			Button.TextColor3 = Color3.fromRGB(255, 255, 255)
			Button.MouseEnter:Connect(function ()
				if not Button.Chosen.Value then
					game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(48, 48, 48)}):Play()
				end
			end)
			Button.MouseMoved:Connect(function ()
				if Button.BackgroundColor3 ~= Color3.fromRGB(48, 48, 48) and not Button.Chosen.Value then
					game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(48, 48, 48)}):Play()
				end
			end)
			Button.MouseLeave:Connect(function ()
				if not Button.Chosen.Value then
					game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(40, 40, 40)}):Play()
				end
			end)
			Button.MouseButton1Click:Connect(function ()
				if not Button.Interactable then return end
				for _, _Button in pairs(Menu.Main.TeamSelect:GetChildren()) do
					if _Button:IsA("TextButton") and _Button.Chosen.Value then
						_Button.Chosen.Value = false
						game.TweenService:Create(_Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(40, 40, 40)}):Play()
						game.TweenService:Create(_Button.UIPadding, TweenInfo.new(0.1), {PaddingTop = UDim.new(0.25, 0), PaddingBottom = UDim.new(0.25, 0)}):Play()
					end
				end
				Button.Chosen.Value = not Button.Chosen.Value
				if Button.Chosen.Value then
					game.TweenService:Create(Button, TweenInfo.new(0.1), {BackgroundColor3 = Color3.fromRGB(55, 55, 55)}):Play()
					game.TweenService:Create(Button.UIPadding, TweenInfo.new(0.1), {PaddingTop = UDim.new(0.225, 0), PaddingBottom = UDim.new(0.225, 0)}):Play()
				else
					game.TweenService:Create(Button.UIPadding, TweenInfo.new(0.1), {PaddingTop = UDim.new(0.25, 0), PaddingBottom = UDim.new(0.25, 0)}):Play()
				end
				script.SelectedTeam.Value = game.Teams[Button.Name]
			end)
		else
			Button.Interactable = false
			Button.BackgroundColor3 = Color3.fromRGB(20, 20, 20)
			Button.TextColor3 = Color3.fromRGB(130, 130, 130)
		end

		Button.Parent = Menu.Main.TeamSelect
	end
	print "3B - LoadTeams finished"
end

Menu.Main.Buttons.Deploy.MouseEnter:Connect(function ()
	print "4 - MouseEnter DeployBtn"
	TweenService:Create(Menu.Main.Buttons.Deploy, TweenInfo.new(0.1), {Size = UDim2.new(0.8, -2, 0.15, -2), BackgroundColor3 = Color3.fromRGB(255, 255, 255), TextColor3 = Color3.fromRGB(10, 10, 10)}):Play()
end)

Menu.Main.Buttons.Deploy.MouseLeave:Connect(function ()
	print "5 - MouseLeave DeployBtn"
	TweenService:Create(Menu.Main.Buttons.Deploy, TweenInfo.new(0.1), {Size = UDim2.new(0.8, 0, 0.15, 0), BackgroundColor3 = Color3.fromRGB(10, 10, 10), TextColor3 = Color3.fromRGB(220, 220, 220)}):Play()
end)

Menu.Main.Buttons.Deploy.MouseButton1Click:Connect(function ()
	print "6 - MouseButton1Click DeployBtn"
	if not Player.Character or Player.Character.Humanoid.Health <= 0 or Player.Team ~= script.SelectedTeam.Value then
		game.ReplicatedStorage.Deploy:FireServer(script.SelectedTeam.Value)
	end
	local Tween1 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(0.5), {BackgroundTransparency = 0})
	Tween1:Play()
	Tween1.Completed:Wait()
	Menu.Visible = false
	local Tween2 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(0.5), {BackgroundTransparency = 1})
	Tween2:Play()
	Tween2.Completed:Wait()
	script.Parent.Enabled = false
end)

game.ReplicatedStorage:WaitForChild("Died").OnClientEvent:Connect(function ()
	print "6 - DiedEvent"
	script.Parent.Enabled = true
	script.Parent.Overlay.BackgroundTransparency = 1
	Menu.Visible = false
	local Tween1 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(5), {BackgroundTransparency = 0})
	Tween1:Play()
	Tween1.Completed:Wait()
	game.ReplicatedStorage.LoadCharacter:FireServer()
	local Tween2 = TweenService:Create(script.Parent.Overlay, TweenInfo.new(0.5), {BackgroundTransparency = 1})
	Tween2:Play()
	Tween2.Completed:Wait()
	script.Parent.Enabled = false
	Menu.Visible = true
end)

LoadTeams()
print "7 - Finished"