Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Team Starter GUIs? read for details

ok so. the title is vague but it is an actual question of mine, team StarterGUIs. Ex. Blue team has a GUI that says "You're on blue!" Red team has its own GUI that says "You're on red!". So far i have this code but it dont work gud, so naturally i came here, here is the code...

view source

01 function teamFromColor(color) 02 for _,t in pairs(game:GetService("Teams"):GetChildren()) do 03 if t.TeamColor==color then return t end 04 end 05 return nil 06 end 07 08 function onSpawned(plr) 09 local tools = teamFromColor(plr.TeamColor):GetChildren() 10 for _,c in pairs(tools) do 11 c:Clone().Parent = plr.PlayerGui 12 end 13 end 14 15 function onChanged(prop,plr) 16 if prop=="Character" then 17 onSpawned(plr) 18 end 19 end 20 21 function onAdded(plr) 22 plr.Changed:connect(function(prop) 23 onChanged(prop,plr) 24 end) 25 end 26 27 game.Players.PlayerAdded:connect(onAdded) any ideas?

1 answer

Hey there, I hope you enjoy this explanation, I've written a lot to try & help you. Let me know if you run into any issues, thanks.

--[[
	First of all, I redid the script a bit.
	Maybe in this way you'll see how you can do better next time.
]]

local function GetTeamFromColor(Color)
	local OrigColor = typeof(color) == "Color3" and color or color.Color
	local Color = BrickColor.new(color)
	-- Above will make it compatible with Color3 arguments AND BrickColor arguments.
	local Teams = {}
	for index,value in pairs(Teams:GetChildren()) do
		if value:IsA("Team") and value.TeamColor == Color then
			table.insert(Teams,value)
		end
	end
	return Teams -- Returns a table of teams.
end

game:GetService("Players").PlayerAdded:Connect(function(Player)
	-- This part may be what confused you.
	-- This is a script that could be improved by a lot.

	-- I'll make it so that when a player's teamcolor changes it gives them new tools and respawns their character.
	local LastTeam = {TeamColor = nil} -- This is here to prevent errors on the first if statement.
	Player:GetPropertyChangedSignal("TeamColor"):Connect(function()
		-- GetPropertyChangedSignal checks for changes in the given property.
		-- Once a change under that property happens, it fires the connection we gave it.
		if Player.TeamColor == LastTeam.TeamColor then
			return -- 'return' is a not only a good way to return information but a good way to halt a function. When using return here, remember that this function will not be used under a variable or etc, so you do not have to return anything specific.

			-- Unlike returning in a function, returning in a connection would only be used to stopping the function and nothing else.
			-- There's plenty of similar cases, but I'm not going to go through them all.
		end
		local PlayerGui = Player:FindFirstChildWhichIsA("PlayerGui")
		local StarterGear = Player:FindFirstChildWhichIsA("StarterGear")
		for index,value in pairs(StarterGear:GetChildren()) do
			-- Remove all the starting gear.
			value:Destroy()
		end
		for index,value in pairs(GetTeamFromColor(Player.TeamColor):GetChildren()) Destroy
			if value:IsA("BackpackItem") then
				value:Clone().Parent = StarterGear -- Cloning all the team items to the StarterGear.
				-- StarterGear is found under the Player.
				-- ex: Players.TheLuminent.StarterGear
			elseif value:IsA("GuiBase2d") then -- If it's a ScreenGui, SurfaceGui (which you won't be needing a SurfaceGui for the PlayerGui, but it's not something to worry about) or GuiMain.
				pcall(function()
					-- This is where it checks wether or not the Gui belongs to the player. (this goes off the team the player is on, using an ObjectValue, this should be possible with the LastTeam variable too but I don't suggest testing with it since I have already done it here)
					if value.TeamItBelongsTo.Value ~= Player.Team then
						value:Destroy()
					end
				end)
				local GuiBase2d = value:Clone()
				GuiBase2d.Parent = PlayerGui

				local TeamValue = Instance.new("ObjectValue") -- Creating an ObjectValue is important here, so that we can check if the ScreenGui belongs to you and not a different team.
				TeamValue.Name = "TeamItBelongsTo"
				TeamValue.Value = Team -- Making the value set to the team so that we can compare it to the Player's team. (refer to line 46)
			end
		end
		Player:LoadCharacter() -- Respawn the player's character.
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.