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

Team change script isnt working. No error?

Works in studio but not in server mode. there is no error so idk what to do, just look it over and tell me what went wrong. this codeis in Normal Script

function joined(Player)
local ShockTrooper = 3867785
local MoA = 3569513
local MP = 3793252
local Protect = 3824300

function civilian()
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("White")
end
function rebel()
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("Bright red")
end
function cadet()
	if Player:IsInGroup(MoA) then
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("New yeller")
	end
	end
function arcanian()
	if Player:IsInGroup(MoA) then
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("Deep orange")
	end
	end
function shock()
	if Player:IsInGroup(ShockTrooper) then
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("Persimmon")
	end
	end
function militarypolice()
	if Player:IsInGroup(MP) then
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("Crimson")
	end
	end
function protectors()
	if Player:IsInGroup(Protect) then
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("CGA brown")
	end
	end
function overlord()
	if Player:GetRoleInGroup(3569513) == 255 then
	Player.Character.Head.Click:Play()
	Player.TeamColor = BrickColor.new("Gold")
	end
	end
function back()
	Player.Character.Head.Click:Play()
	script.Parent.Parent.Teams.Visible = false
	script.Parent.Parent.Play.Visible = true
end
end

script.Parent.Civilian.MouseButton1Click:connect(civilian)
script.Parent.Rebel.MouseButton1Click:connect(rebel)
script.Parent.Cadet.MouseButton1Click:connect(cadet)
script.Parent.Arcanian.MouseButton1Click:connect(arcanian)
script.Parent["Shock Trooper"].MouseButton1Click:connect(shock)
script.Parent["Military Police"].MouseButton1Click:connect(militarypolice)
script.Parent.Protector.MouseButton1Click:connect(protectors)
script.Parent.OverLord.MouseButton1Click:connect(overlord)
script.Parent.Back.MouseButton1Click:connect(back)
game.Players.ChildAdded:connect(joined)

3 answers

You're trying to use PlayerAdded on a script that doesn't need that Event. You're also trying to call your functions outside of their scope. The functions are inside the PlayerAdded function and you're calling them from outside that function. You're also failing to properly load an animation.

You can simply put a LocalScript inside your GUI and remove that event and the function wrapping the rest of your functions. However, if FilteringEnabled is on you're going to have to utilize a RemoteEvent to actually change your team, else no one else will see the change but you.

This automates your process by editing the teams table.


local entry = script.Parent -- root location 

local me = game.Players.LocalPlayer
local character = me.Character or me.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local head = character:WaitForChild("Head")

local ShockTrooper = 3867785
local MoA = 3569513
local MP = 3793252
local Protect = 3824300

local backButton -- setup this 


-- Your id - Make sure the animation you're using is using the same BodyPart names
	-- aka the same rig ; robloxian 2.0 vs original body rig is different
local animationId = "rbxassetid://1242988946"
-- Parent the animation
local animation = Instance.new("Animation", head)
-- Set the AnimationId
animation.AnimationId = animationId
-- pre-load the animation ( THIS IS REQUIRED )
local loaded = humanoid:LoadAnimation(animation)


-- This is a dictionary!
-- Name the Names the same as your button names
local teams = {
	
	-- Supply a groupId if we need to check if we're in a group
	-- Supply a groupRank if a rank is required
	
	['civilian'] = { teamColor = BrickColor.White() };
	['rebel'] = { teamColor = BrickColor.Red() };
	['cadet'] = { groupId = MoA, teamColor = BrickColor.new("New yeller") };
	['arcanian'] = { groupId = MoA, teamColor = BrickColor.new("Deep orange") };
	['shock'] = { groupId = MoA, teamColor = BrickColor.new("Persimmon") };	
	['militarypolice'] = { groupId = MP, teamColor = BrickColor.new("Crimson") };
	['protectors'] = { groupId = Protect, teamColor = BrickColor.new("CGA brown") };
	['overlord'] = { groupId = MoA, groupRank = 255, groupRole = "Owner", teamColor = BrickColor.new("Gold") };
		
}


local function println(message, tupple)
	-- string.format is used here to replace %s with what's in the table
		-- with what is unpacked with unpack()
			-- unpack( the tupple supplied or an empty table if not )
	print( string.format( message, unpack( tupple or {} ) ) )
end

local function setup(teamColor)
	if teamColor then 
		me.TeamColor = teamColor -- Utilize RemoteEvent here
	end
	loaded:Stop()
	loaded:Play()
	-- play animation
end

local function buttonClicked(button)
	local index = teams[button.Name]
	
	-- :Ternary operators to shorten if, else, elseif statements:
	-- if there's a group id
		-- if we're in the group set to true
			-- else set to false
	local playerInGroup = index.groupId and me:IsInGroup(index.groupId) or false
	-- Same as above but with role
	local roleInGroup = index.groupRank and playerInGroup and me:GetRoleInGroup(index.groupId) or false 
	-- Same as above but with rank
	local myRank = playerInGroup and index.groupRank and me:GetRankInGroup(index.groupId)
	local minimumRank = index.groupRank
	local teamColor = index.teamColor or false 
	
	
	-- Supply a table or tupple to println (read more in setup)
	println( "%s is %s group [ %s ] and his/her rank is [ %s : %s ]", {
		
		me.Name,
		playerInGroup and "in" or "not in",
		index.groupId or "no group limitation",
		roleInGroup or "no role limitations",
		myRank or "no role limitations"
		
	} )
	
	-- Using the information supplied above
		-- Why it's a great idea to use variables for use later!
	if index.groupId then
		if playerInGroup then 
			if myRank then 
				-- if there is a minimumRank in place and we have it, setup.
				setup(teamColor)
			else 
				-- else if there's not minimumRank, we're still in the group, so setup.
				setup(teamColor)
			end
		else
			-- not in group
		end
	else
		-- no group checking (civ or rebel)
		setup(teamColor)
	end
end

-- this loops through everything in your gui ( if entry is setup correctly )
	-- and finds the button names that match what is in the teams table
local function crawl(obj)
	-- loop through obj
	for i,v in pairs(obj:GetChildren()) do
		-- if the objects name equals the table name
			-- then it must be a button that we have configured in teams
		if teams[v.Name] then 
			local button = v
			button.MouseButton1Down:Connect(function()
				buttonClicked(button)
			end)
		end
		crawl(v)
	end
end


crawl(entry)


For me I can't change the players team using teamcolor I need to do it like this:

Player.Team = game.Teams:WaitForChild("The teams name")

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