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

team script not working?

My goal: if youre admin and you type ":teams"then it will make a blue and a red team on the leaderboard Problem:the above does not happen i get no errors

admins={"Threatboy101","vinothpizza555"}


function chat(msg, player)
    for index, value in pairs(admins) do
        if player.Name:lower() == value:lower() then
            if string.lower(msg)==":teams"then
            teams = game:GetService("Teams")
            blue = Instance.new("Team", teams)
            blue.Name = "BLUE"
            blue.TeamColor = BrickColor.new("Bright blue")
            red = Instance.new("Team", teams)
            red.Name="RED"
            red.TeamColor = BrickColor.new("Bright red")
        end
    end
end

game.Players.PlayerAdded:connect(function(player)    
    player.Chatted:connect(chat)
end)

2 answers

✓ Accepted answer

Your script is pretty disorganized, I'll fix it up for you. Here:

local Admins = {} --Put admins here

function IsAdmin(Plyr) --A simple function that'll return true if "Plyr" is an Admin
	for _,v in pairs(Admins) do
		if Plyr.Name:lower() == v:lower() then
			return true
		end
	end
	return false
end

game.Players.PlayerAdded:connect(function(Plyr)
	if IsAdmin(Plyr) then --If the player is an Admin, it'll give them the chat command
		Plyr.Chatted:connect(function(Msg)
			if Msg:lower() == ":teams" then
				local Teams = game:GetService("Teams")

				local Blue_Team = Instance.new("Team")
				Blue_Team.Name = "BLUE"
				Blue_Team.TeamColor = BrickColor.new("Bright blue")
				Blue_Team.Parent = Teams

				local Red_Team = Instance.new("Team")
				Red_Team.Name = "RED"
				Red_Team.TeamColor = BrickColor.new("Bright red")
				Red_Team.Parent = Teams
			end
		end)
	end
end)

NOTE: When the teams are created, all the players are going to be neutral because their teamcolors were never changed. You'll have to do that yourself.

Hope this helped!

1 comment

0

thanks and i figured how to do the neutral thing was a simple puzzle

The actual problem is the player.Chatted event only has one parameter - the message that was chatted. Because of that, every time the chat function ran, player was nil. Line 6 should definitely have given you an error if you ran the script in online mode, because it would try to access the "Name" property of nil. Did you press F9 and look at the Server output, or were you looking at the Local output?

TurboFusion fixed that in his answer by putting the player variable inside the PlayerAdded event handler, so player was defined, but he didn't explain that was the problem.

To fix it, change line 20 from: player.Chatted:connect(chat) to: player.Chatted:connect(function(msg) chat(msg, player) end)

(This is an anonymous function that passes the player variable to the chat function instead of just the message)

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