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

How do I assign players to teams?

I'm not sure where to start, I want to assign a group of players to multiple teams all at once. I want to be able to mention multiple names at once like

:team Builderman,Person299,Bowanw,Shedletsky Red

or even assign half the players from one team to another (example, neutral to Blue)

:team %random% Blue

I'm only used to doing one argument commands, so any help is appreciated...

This is my admin command script so far, to get you an idea on how I've set things up.

-- Admins
Admins = {"bowanw","Player1"} -- Admin List.
Prefix = ":" -- This allows you to change the prefix of each command.


function checkAdmin(player) -- This function checks that the player using the commands, is an Admin
	for i=1,table.getn(Admins) do
		 if player.Name:lower() == Admins[i]:lower() then
			return true 
		end
	end
end




--[[
	Current List of Commands:
	:open	Opens Training Room Outside Door
	:close	Closes Training Room Outside Door
	:load <Item>	Loads item into the game
	:unload <Item>	Unloads item from the game
--]]

game.Players.PlayerAdded:connect(function(player) -- This runs a function as soon as the player joins
	if checkAdmin(player) then -- Checks that the player is an Admin
    player.Chatted:connect(function(msg)
	
	
	
	
	
	--These functions are what run the commands.
	
	
	function CloseDoor(bool)
		if bool then
			workspace["Training Assembly Room"].Door.Transparency = 0
			workspace["Training Assembly Room"].Door.CanCollide = true
		else
			workspace["Training Assembly Room"].Door.Transparency = 1
			workspace["Training Assembly Room"].Door.CanCollide = false
		end
	end
	
	
	function Load(Str)
		if game.ServerStorage:FindFirstChild(Str) then
			local part = game.ServerStorage:FindFirstChild(Str):Clone()
			part.Parent = workspace
		end
	end
	
	function UnLoad(Str)
		if game.ServerStorage:FindFirstChild(Str) and workspace:FindFirstChild(Str) then -- Its neccesary to still check the server storage, otherwise we may delete other parts we do not want to.
			workspace:FindFirstChild(Str):Destroy()
		end
	end
	
	
	
	
	
	-- These are the if statements that trigger the functions that run the commands.
	
	if msg:sub(1,5):lower() == Prefix.."open" then
		CloseDoor(false)
	end
	
	if msg:sub(1,6):lower() == Prefix.."close" then
		CloseDoor(true)
	end
	
	if msg:sub(1,5):lower() == Prefix.."load" then
		Load(msg:sub(7))
	end
	
	if msg:sub(1,7):lower() == Prefix.."unload" then
		UnLoad(msg:sub(9))
	end
	
    end)
end
end)

4 comments

0

There's a property in each player called "TeamColor" which behaves similar to BrickColor in parts. With the putting multiple people in the command, you can take a look at strings and how they work for that.

0

Watch the language! You can read our community guidelines here: https://scriptinghelpers.org/help/community-guidelines/

0

@M39a9am3r, I'm sorry mate... but what language have I used that is deemed inappropriate?

0

@Tempestatem, I understand how to assign a player to a team, my question is how I would set this up for a command. To use more than one argument...

1 answer

✓ Accepted answer

I tried to improve your admin script in general. I had to make a special case for your team change command with a player list. It requires string patterns which are hard to explain. I hope this helps you out.

I didn't bother implementing partial input (For names etc) but it is easy to add yourself. Good luck!

--* you should use UserId rather than names; names can change.
--* and set them to true so you do not need the checkAdmin function.

Admins = {[1103610]=true,[-1]=true}
Prefix = ":"

--* you should define your small functions outside of the main function.
--* I've done some optimization.

 function CloseDoor(bool)
    workspace["Training Assembly Room"].Door.Transparency = (bool and 0) or 1
    workspace["Training Assembly Room"].Door.CanCollide = bool
end
function Load(Str)
	local part = game.ServerStorage:FindFirstChild(Str)
    if part then
        part:Clone().Parent = workspace
    end
end
function UnLoad(Str)
	local part = workspace:FindFirstChild(Str)
    if part and game.ServerStorage:FindFirstChild(Str) then
        part:Destroy()
    end
end
function ChangeTeam(list,team)
	team = game.Teams:FindFirstChild(team)
	if team then
		for _,str in pairs(list) do
			local p = game.Players:FindFirstChild(str)
			if p then
				p.Team = team
			end
		end
	end
end

--* I recommend you store your commands in a table like this to loop through.
--* Automate everything by storing function and parameter.
local cmds0 = {open = {CloseDoor,true} , close = {CloseDoor,false} , load = {Load,'input'} , unload = {UnLoad,'input'} , team = {ChangeTeam,'list','input'}}

--* auto add the prefix and space like this
local cmds = {}
for cmd,tab in pairs(cmds0) do
	cmds[Prefix..cmd..' '] = tab
	cmds0[cmd]=nil
end

--* for the teamchange list parameter you'll have to use string manipulation to find names.

function onPlayerAdded(player)
	if Admins[player.UserId] then
	    player.Chatted:connect(function(msg)
			local lowered = msg:lower()
			for cmd,tab in pairs(cmds) do
				if lowered:sub(1,#cmd) == cmd then
					
					local fun = tab[1]
					local param = tab[2]
					local rest = msg:sub(#cmd+1)
					
					if param == 'input' then
						param = rest
					elseif param == 'list' then
						local list = {}
						local pattern = "%w+[_%w+]*[%s,]" --match names including underscores that end with comma or space
						for str in string.gmatch(rest,pattern) do
							table.insert(list,str:sub(1,#str-1)) --remove comma
						end
						param = list
						
						if tab[3] == 'input' then
							fun(list,rest:match("%w+$")) --match last string in the message
							break
						end
					end
					
					fun(param)
					
					break
				end
			end
	    end)
	end
end

game.Players.PlayerAdded:connect(onPlayerAdded)

2 comments

0

Thankyou very much, also... for some reason, the ':close' and ':open' commands now only work as ':open true' & ':close false'

0

Don't type an input, and it should use the parameter you specify like in {CloseDoor,true} automatically.

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