Team Assign Command Not Working

Hello! So I have created a code which is 2 parts, a LocalScript and ServerScript. The LocalScript’s location is in StarterGui, and the ServerScript’s location is in ServerScriptService. There is a RemoteEvent in ReplicatedStorage as well. The script is not working, and I don’t really know what’s happening.

LocalScript:

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.sub(Message, 1, 11) == "!TeamAssign" and Player:GetRankInGroup(11941815) >= 15 and Player.TeamColor == BrickColor.new("Pastel orange") then
			game.ReplicatedStorage.AssignTeams:FireServer()
		end
	end)
end)

ServerScript:

game.ReplicatedStorage.AssignTeams.OnServerEvent:Connect(function()
	game.Players.PlayerAdded:Connect(function(Player)
		local GroupID = 11941815
		
		if Player:GetRankInGroup(GroupID) == 5 then
			Player.TeamColor = BrickColor.new("Salmon")
		else
			if Player:GetRankInGroup(GroupID) == 6 then
				Player.TeamColor = BrickColor.new("Sage green")
			else
				if Player:GetRankInGroup(GroupID) == 7 then
					Player.TeamColor = BrickColor.new("Buttermilk")
				end
			end
		end
	end)
end)

From what I understand, this script should be working. (Also, please don’t send any other links from the DevForum, as this system is custom to fit my game.) Thanks in advance!

Couldn’t you just do all of that in the server, it can even prevent exploits if u do that

plr.Chatted dosent require to be in local, it can be server

1 Like

I could, however, I first need to get chats, which is better to handle locally.

1 Like

But less safe due to exploits, just try server sided

1 Like

Very well, I will try server sided, and will get back to you with the resolution.

1 Like

I see your issue regarding this script and I have a new script relative to this one that has the same objective you are trying to do.

I recommend re starting the script with this:

game.Players.PlayerAdded:Connect(function(player)
for ids,team in pairs(groups) do
local rankInGroup = player:GetRankInGroup(ids)
if rankInGroup > 250 then
player.Team = game.Teams[team] --Change this to the specific team
elseif rankInGroup > 0 then
player.Team = game.Teams[team]
end
end
end)

1 Like

Making things server-sided does not solve all the issues with exploiters, running checks on the stuff sent from the client is a better method.

1 Like

Dosent matter, you don’t need remoteevent junks for this

Plus, I don’t see how it’s better like that

1 Like

Capture
This should be able to work.

1 Like

I had tried using a for loop (for i = 1, #Players do) with the players being a variable of game.Players:GetPlayers, however I scrapped it once the script didn’t work. Perhaps I should try adding prints?

1 Like

You can try that but the photo I send above seems to work for me. Insert the group ID in player:Getrankingroup(ids).

1 Like

Would this work? Because if so, It’s a problem with the LocalScript.

game.ReplicatedStorage.AssignTeams.OnServerEvent:Connect(function()
	
	local GroupID = 11941815
	local Players = game.Players:GetPlayers()
	
	for i = 1, #Players do	
		local CurrentPlayer = Players[i]
		
		if CurrentPlayer:GetRankInGroup(GroupID) == 5 then
			CurrentPlayer.TeamColor = BrickColor.new("Salmon")
		else
			if CurrentPlayer:GetRankInGroup(GroupID) == 6 then
				CurrentPlayer.TeamColor = BrickColor.new("Sage green")
			else
				if CurrentPlayer:GetRankInGroup(GroupID) == 7 then
					CurrentPlayer.TeamColor = BrickColor.new("Buttermilk")
				end
			end
		end
	end
end

The for loop is basically going through 1 player at a time, then it continues the script until it finds their rank and teams them with that rank color.

1 Like

I’m not sure. But there’s scripts that are posted in the devfourm on scripting teams that work.

1 Like

I’m debugging the LocalScript to see if the problems there.

1 Like

Okay, it should work but tell me if it doesn’t.

1 Like

I don’t get why you listen to new players when the RemoteEvent is fired. How is this team assign supposed to work?

1 Like

Just as suspected earlier, the LocalScript has an error, somewhere near the string.sub part.

1 Like

So I fixed that, I realized that was a bad idea, hence making me use a for loop.

1 Like
local Players = game:GetService("Players")
local Storage = game:GetService("ReplicatedStorage")
local TeamEvent = Storage:WaitForChild("AssignTeams")

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.lower(string.sub(Message, 1, 11)) == "!teamassign" and Player:GetRankInGroup(11941815) >= 15 and Player.TeamColor == BrickColor.new("Pastel orange") then
			TeamEvent:FireServer()
		end
	end)
end)
local GroupID = 11941815
local Storage = game:GetService("ReplicatedStorage")
local TeamEvent = Storage:WaitForChild("AssignTeams")
TeamEvent.OnServerEvent:Connect(function(Player)
	if Player:GetRankInGroup(GroupID) == 5 then
		Player.TeamColor = BrickColor.new("Salmon")
	elseif Player:GetRankInGroup(GroupID) == 6 then
		Player.TeamColor = BrickColor.new("Sage green")
	elseif Player:GetRankInGroup(GroupID) == 7 then
		Player.TeamColor = BrickColor.new("Buttermilk")
	end)
end)

I’ve made a few slight, changes, I’ve added waits to make sure instances load before the script is executed, I’ve made it so that if different capitalisations of the same message are chatted the script will still detect them, I’ve removed the PlayerAdded event from the OnServerEvent event handler as it wouldn’t work, whenever “FireServer()” is called from a client script, the player instance pertinent to that client is always automatically passed as an argument to any function connected to the corresponding OnServerEvent event handler, as such all we need to do is simply declare a parameter in the connected function to handle this passed object. I’ve also changed the logic of the conditional statements, “elseif” allows you to check multiple conditions within the same conditional chain.

Is there a mistake here?

It’s making it check if the Rank is more or equal to 15 and then it fires the event. In the event script, it only checks if the rank is 5, 6, or 7, which makes it completely useless.

@ItsKoolPlayz Did you mean this:

Player:GetRankInGroup(11941815) >= 5