Simplify and Fix this Script

So I have this script that’s a part of a GUI that changes the player’s team, but it has a few problems. The big one is as you can see from taking one look at it, it’s highly inefficient. I tried doing it with a table before but it broke, even more, this works for now. In addition, the script does not even work, and I have no idea why since there aren’t any errors in the output. So I was wondering A. how to make it more efficient and less complicated, and B. How to make it work. The part that does not work is the detection of the click.

Script:

local team = "Class D"
local teamVal = script.Parent.Team
local morph = "test"

while true do
	wait(0.001)
	if teamVal.Value == 1 then
		team = "Class D"
		script.Parent.Button2.Visible = false
	end
	if teamVal.Value == 2 then
		team = "Foundation Personnel"
		script.Parent.Button2.Visible = true
	end
	if teamVal.Value == 3 then
		team = "Security Department"
	end
	if teamVal.Value == 4 then
		team = "Medical Department"
	end
	if teamVal.Value == 5 then
		team = "Scientific Department"
	end
	if teamVal.Value == 6 then
		team = "Ethics Committee"
	end
	if teamVal.Value == 7 then
		team = "Department of External Affairs"
	end
	if teamVal.Value == 8 then
		team = "Mobile Task Force"
	end
	if teamVal.Value == 9 then
		team = "Rapid Response Team"
	end
	if teamVal.Value == 10 then
		team = "Internal Security Department"
	end
	if teamVal.Value == 11 then
		team = "Intelligence Agency"
	end
	if teamVal.Value == 12 then
		team = "Facility Executive"
	end
	if teamVal.Value == 13 then
		team = "Chaos Insurgency"
	end
	if teamVal.Value == 14 then
		team = "SCP"
		script.Parent.Button1.Visible = true
	end
	if teamVal.Value == 15 then
		team = "O5 Command"
		script.Parent.Button1.Visible = false
	end
	script.Parent["Team Lable"].Text = "Current Team Selected: "..team..""

end

local Players = game:GetService("Players")



-----
game.StarterGui.MainMenu.GUIVis.Value = 1

local tweenservice = game:GetService("TweenService")
local fadebackground = script.Parent.Parent.Black



local tweenInfo = TweenInfo.new(
	0.2,--time in seconds
	Enum.EasingStyle.Sine, --smooth
	Enum.EasingDirection.Out, --dont change
	0, -- dont change
	true, --reverse
	0
)

local tween = tweenservice:Create(fadebackground, tweenInfo, {BackgroundTransparency = 0})
local player = game.Players.LocalPlayer


function leftClick()
	player.Team = team
	tween:Play()
	game:GetService("Players").LocalPlayer.Character.Humanoid.Health=0
	script.Parent.EffectHover:Play()
	wait(0.2)
	script.Parent.Visible = false
	script.Parent.Parent.Main.Visible = true
	script.Parent.Parent.Enabled = false
	game.Lighting.MenuBlur.Size = 0
	game.Workspace.Camera.CameraType = Enum.CameraType.Custom
	game.StarterGui.MainMenu.GUIVis.Value = 0
end


script.Parent.Play.TextButton.MouseButton1Click:Connect(leftClick)
local team = "Class D"
local teamVal = script.Parent.Team
local  morph = "test"
while true do
	wait(0.001)
	if teamVal.Value == 1 then
		team = "Class D"
		script.Parent.Button2.Visible = false
	end
	if teamVal.Value == 2 then
		team = "Foundation Personnel"
		script.Parent.Button2.Visible = true
	end
	if teamVal.Value == 3 then
		team = "Security Department"
	end
	if teamVal.Value == 4 then
		team = "Medical Department"
	end
	if teamVal.Value == 5 then
		team = "Scientific Department"
	end
	if teamVal.Value == 6 then
		team = "Ethics Committee"
	end
	if teamVal.Value == 7 then
		team = "Department of External Affairs"
	end
	if teamVal.Value == 8 then
		team = "Mobile Task Force"
	end
	if teamVal.Value == 9 then
		team = "Rapid Response Team"
	end
	if teamVal.Value == 10 then
		team = "Internal Security Department"
	end
	if teamVal.Value == 11 then
		team = "Intelligence Agency"
	end
	if teamVal.Value == 12 then
		team = "Facility Executive"
	end
	if teamVal.Value == 13 then
		team = "Chaos Insurgency"
	end
	if teamVal.Value == 14 then
		team = "SCP"
		script.Parent.Button1.Visible = true
	end
	if teamVal.Value == 15 then
		team = "O5 Command"
		script.Parent.Button1.Visible = false
	end
	script.Parent["Team Lable"].Text = "Current Team Selected: "..team..""
end    --[[---]]
game.StarterGui.MainMenu.GUIVis.Value = 1
local tweenservice = game:GetService("TweenService")
local fadebackground = script.Parent.Parent.Black
local tweenInfo = TweenInfo.new( 	0.2, --[[time in seconds]] 	Enum.EasingStyle.Sine, --[[smooth]] 	Enum.EasingDirection.Out, --[[dont change]] 	0, --[[ dont change]] 	true, --[[reverse]] 	0 )
function leftClick()
end

Here ya go, mate!

The only problem is now I no longer have the tween variable to play the tween when clicked. Also out of curiosity, I was wondering if you knew if there was any way to condense the first part from a ton of if statements to a table, I’m not a very advanced scripter so I have no idea if it is or not, just wondering.


local c = "Class D"
local b = script.Parent.Team
local  a = "test"
while true do
	wait(0.001)
	if b.Value == 1 then
		c = "Class D"
		script.Parent.Button2.Visible = false
	end
	if b.Value == 2 then
		c = "Foundation Personnel"
		script.Parent.Button2.Visible = true
	end
	if b.Value == 3 then
		c = "Security Department"
	end
	if b.Value == 4 then
		c = "Medical Department"
	end
	if b.Value == 5 then
		c = "Scientific Department"
	end
	if b.Value == 6 then
		c = "Ethics Committee"
	end
	if b.Value == 7 then
		c = "Department of External Affairs"
	end
	if b.Value == 8 then
		c = "Mobile Task Force"
	end
	if b.Value == 9 then
		c = "Rapid Response Team"
	end
	if b.Value == 10 then
		c = "Internal Security Department"
	end
	if b.Value == 11 then
		c = "Intelligence Agency"
	end
	if b.Value == 12 then
		c = "Facility Executive"
	end
	if b.Value == 13 then
		c = "Chaos Insurgency"
	end
	if b.Value == 14 then
		c = "SCP"
		script.Parent.Button1.Visible = true
	end
	if b.Value == 15 then
		c = "O5 Command"
		script.Parent.Button1.Visible = false
	end
	script.Parent["Team Lable"].Text = "Current Team Selected: "..c..""
end    --[[---]]
game.StarterGui.MainMenu.GUIVis.Value = 1
local a = game:GetService("TweenService")
local a = script.Parent.Parent.Black
local a = TweenInfo.new( 	0.2, --[[time in seconds]] 	Enum.EasingStyle.Sine, --[[smooth]] 	Enum.EasingDirection.Out, --[[dont change]] 	0, --[[ dont change]] 	true, --[[reverse]] 	0 )
function leftClick()
end

Like this?

Cleaned up that mess:

local tweenService = game:GetService("TweenService")
local lighting = game:GetService("Lighting")
local _workspace = game:GetService("Workspace")
local players = game:GetService("Players")

local player = players.LocalPlayer
local teamVal = script.Parent.Team
local team = "Class D"

local teams = {
	[1] = "Class D",
	[2] = "Foundation Personnel",
	[3] = "Security Department",
	[4] = "Medical Department",
	[5] = "Scientific Department",
	[6] = "Ethics Committee",
	[7] = "Department of External Affairs",
	[8] = "Mobile Task Force",
	[9] = "Rapid Response Team",
	[10] = "Internal Security Department",
	[11] = "Intelligence Agency",
	[12] = "Facility Executive",
	[13] = "Chaos Insurgency",
	[14] = "SCP",
	[15] = "OS Command"
}

teamVal:GetPropertyChangedSignal("Value"):Connect(function()
	if not table.find(teams, teamVal.Value) then return end

	team = teams[teamVal.Value]
	script.Parent["Team Lable"].Text = "Current Team Selected: "..team..""
end)

player.PlayerGui.MainMenu.GUIVis.Value = 1

script.Parent.Play.TextButton.MouseButton1Click:Connect(function()
	player.Team = team

	local tween = tweenService:Create(script.Parent.Parent.Black,
		TweenInfo.new(
			0.2,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true
		), {BackgroundTransparency = 0}
	):Play()
	
	local a, e = pcall(function()
		player.Character.Humanoid.MaxHealth = 0
		player.Character.Humanoid.Health = 0
		
		script.Parent.EffectHover:Play()
		
		wait(0.2)
		
		script.Parent.Visible = false
		script.Parent.Parent.Main.Visible = true
		script.Parent.Parent.Enabled = false
		
		lighting.MenuBlur.Size = 0
		_workspace.Camera.CameraType = Enum.CameraType.Custom
		
		player.PlayerGui.MainMenu.GUIVis.Value = 0
	end)
	
	if not a then
		warn(string.format("Something went wrong :C \n %s", e))
	end
end)

I’m getting an error on line 38

 11:59:25.595  Players.DDZ_76.PlayerGui.MainMenu.TeamSelect.ValueScript:38: invalid argument #3 (Instance expected, got string)  -  Client - ValueScript:38

Line 38 is

player.Team = team

Are you sure about that?


local L_1_ = game:GetService("TweenService")
local L_2_ = game:GetService("Lighting")
local L_3_ = game:GetService("Workspace")
local L_4_ = game:GetService("Players")

local L_5_ = L_4_.LocalPlayer
local L_6_ = script.Parent.Team
local L_7_ = "Class D"

local L_8_ = {
	[1] = "Class D",
	[2] = "Foundation Personnel",
	[3] = "Security Department",
	[4] = "Medical Department",
	[5] = "Scientific Department",
	[6] = "Ethics Committee",
	[7] = "Department of External Affairs",
	[8] = "Mobile Task Force",
	[9] = "Rapid Response Team",
	[10] = "Internal Security Department",
	[11] = "Intelligence Agency",
	[12] = "Facility Executive",
	[13] = "Chaos Insurgency",
	[14] = "SCP",
	[15] = "OS Command"
}

L_6_:GetPropertyChangedSignal("Value"):Connect(function()
	if not table.find(L_8_, L_6_.Value) then
		return
	end

	L_7_ = L_8_[L_6_.Value]
	script.Parent["Team Lable"].Text = "Current Team Selected: "..L_7_..""
end)

L_5_.PlayerGui.MainMenu.GUIVis.Value = 1

script.Parent.Play.TextButton.MouseButton1Click:Connect(function()
	L_5_.Team = L_7_

	local L_9_ = L_1_:Create(script.Parent.Parent.Black,
		TweenInfo.new(
			0.2,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true
		), {
		BackgroundTransparency = 0
	}
	):Play()

	local L_10_, L_11_ = pcall(function()
		L_5_.Character.Humanoid.MaxHealth = 0
		L_5_.Character.Humanoid.Health = 0

		script.Parent.EffectHover:Play()

		wait(0.2)

		script.Parent.Visible = false
		script.Parent.Parent.Main.Visible = true
		script.Parent.Parent.Enabled = false

		L_2_.MenuBlur.Size = 0
		L_3_.Camera.CameraType = Enum.CameraType.Custom

		L_5_.PlayerGui.MainMenu.GUIVis.Value = 0
	end)

	if not L_10_ then
		warn(string.format("Something went wrong :C \n %s", L_11_))
	end
end)

Could this work?

Whenever I click the button in my GUI, yes.

Oh, I see my problem now - The team I set the player to has to be an actual team Instance. Here, this should work.

local tweenService = game:GetService("TweenService")
local lighting = game:GetService("Lighting")
local _workspace = game:GetService("Workspace")
local players = game:GetService("Players")
local teams = game:GetService("Teams")

local player = players.LocalPlayer
local teamVal = script.Parent.Team
local team = "Class D"

local teams = {
	[1] = "Class D",
	[2] = "Foundation Personnel",
	[3] = "Security Department",
	[4] = "Medical Department",
	[5] = "Scientific Department",
	[6] = "Ethics Committee",
	[7] = "Department of External Affairs",
	[8] = "Mobile Task Force",
	[9] = "Rapid Response Team",
	[10] = "Internal Security Department",
	[11] = "Intelligence Agency",
	[12] = "Facility Executive",
	[13] = "Chaos Insurgency",
	[14] = "SCP",
	[15] = "OS Command"
}

teamVal:GetPropertyChangedSignal("Value"):Connect(function()
	if not table.find(teams, teamVal.Value) then return end

	team = teams[teamVal.Value]
	script.Parent["Team Lable"].Text = "Current Team Selected: "..team..""
end)

player.PlayerGui.MainMenu.GUIVis.Value = 1

script.Parent.Play.TextButton.MouseButton1Click:Connect(function()
	player.Team = teams[team]

	local tween = tweenService:Create(script.Parent.Parent.Black,
		TweenInfo.new(
			0.2,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true
		), {BackgroundTransparency = 0}
	):Play()

	local a, e = pcall(function()
		player.Character.Humanoid.MaxHealth = 0
		player.Character.Humanoid.Health = 0

		script.Parent.EffectHover:Play()

		wait(0.2)

		script.Parent.Visible = false
		script.Parent.Parent.Main.Visible = true
		script.Parent.Parent.Enabled = false

		lighting.MenuBlur.Size = 0
		_workspace.Camera.CameraType = Enum.CameraType.Custom

		player.PlayerGui.MainMenu.GUIVis.Value = 0
	end)

	if not a then
		warn(string.format("Something went wrong :C \n %s", e))
	end
end)

Although there is not an error for it, the part of my script that changes a GUI label based on what team has been selected doesn’t work anymore. Also the part of the system that is used to change which team you are going to select also no longer works. Although the Select team button does work along with tweens, the rest of the team select system does not.

you wouldn’t use a string for this but a table i believe

You can, and it’s the best way to get rid of those else-ifs.

Fixed that GUI part. I don’t know whether you mean changing the teams no longer works but here’s the fix for the labels

local tweenService = game:GetService("TweenService")
local lighting = game:GetService("Lighting")
local _workspace = game:GetService("Workspace")
local players = game:GetService("Players")
local teams = game:GetService("Teams")

local player = players.LocalPlayer
local teamVal = script.Parent.Team
local team = "Class D"

local teams = {
	[1] = "Class D",
	[2] = "Foundation Personnel",
	[3] = "Security Department",
	[4] = "Medical Department",
	[5] = "Scientific Department",
	[6] = "Ethics Committee",
	[7] = "Department of External Affairs",
	[8] = "Mobile Task Force",
	[9] = "Rapid Response Team",
	[10] = "Internal Security Department",
	[11] = "Intelligence Agency",
	[12] = "Facility Executive",
	[13] = "Chaos Insurgency",
	[14] = "SCP",
	[15] = "O5 Command"
}

teamVal:GetPropertyChangedSignal("Value"):Connect(function()
	if not table.find(teams, teamVal.Value) then return end
	
	if teamVal.Value == "Class D" or teamVal.Value == "O5 Command" then
		script.Parent.Button1.Visible = false
	end
	
	if teamVal.Value == "Foundation Personnel" or teamVal.Value == "SCP" then
		script.Parent.Button1.Visible = true
	end

	team = teams[teamVal.Value]
	script.Parent["Team Lable"].Text = "Current Team Selected: "..team..""
end)

player.PlayerGui.MainMenu.GUIVis.Value = 1

script.Parent.Play.TextButton.MouseButton1Click:Connect(function()
	player.Team = teams[team]

	local tween = tweenService:Create(script.Parent.Parent.Black,
		TweenInfo.new(
			0.2,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true
		), {BackgroundTransparency = 0}
	):Play()

	local a, e = pcall(function()
		player.Character.Humanoid.MaxHealth = 0
		player.Character.Humanoid.Health = 0

		script.Parent.EffectHover:Play()

		wait(0.2)

		script.Parent.Visible = false
		script.Parent.Parent.Main.Visible = true
		script.Parent.Parent.Enabled = false

		lighting.MenuBlur.Size = 0
		_workspace.Camera.CameraType = Enum.CameraType.Custom

		player.PlayerGui.MainMenu.GUIVis.Value = 0
	end)

	if not a then
		warn(string.format("Something went wrong :C \n %s", e))
	end
end)


Maybe this will help, the top part is supposed to say whatever team is selected, for example, “Current Team Selected: Mobile Task Force”. Also, the arrows at the bottom were supposed to change whichever team was selected in addition to hiding for example the righthand arrow if you were at the last team and lefthand if you were at the first. Kind of make sense?

I believe the issue is with line 29, no errors in the output, it just seems logical that that is where it would be.

don’t use getpropertychanged signal, just use .Changed, in my experience of using both GetPropertyChangedSignal barely ever works. Changed passes the changed value as an argument, if you really need it. And what kind of value is the teamVal? You should try to make it an IntValue, NumberValues can be decimals from what I remember.

How would you recommend writing that line of script, whenever I try I cause an error so I am probably doing something wrong.

Have any idea how to fix the script?