Change team not working for admins

My script works perfectly for all teams, but with the admin team, even if the player’s name is not in the table, they can still join the team. Anyone got any solutions?

local Admins = {}

game.ReplicatedStorage.Remotes.ChangeTeam.Prisoner.OnServerEvent:Connect(function(player, teamColor)
	player.TeamColor = teamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Police.OnServerEvent:Connect(function(player, teamColor)
	player.TeamColor = teamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Admin.OnServerEvent:Connect(function(player, teamColor)
	for i,v in pairs(Admins) do
		if player.Name == v then
			player.TeamColor = teamColor
			wait(0.01)
			player:LoadCharacter()
		end
	end	
end)

For admin lists, you should use their UserId instead of their username because they can change their name at any time but they can’t change their UserId.

In the code you provided, it’s correct but the list is empty.

I’ve changed it to the userid, but still anyone can go onto that team.

You could do this:

if table.find(Admins, player.UserId) then
  -- do stuff
end

Still doesn’t work. Any player can go onto the team

I would recommend fixing up your remote events around as from the looks of it, it can be easily exploited to join the admins by firing the prisoner or police remote and just giving it the teamcolor of admins. Is that what you’re referring to?

1 Like

Yes, when they click the join button this gets fired:

game.ReplicatedStorage.Remotes.ChangeTeam.Prisoner:FireServer(BrickColor.new("Plum"))

Wait I think I found the problem

Is Plum is the teamColor of admins?

The problem was that I fired the prisoner event instead of the admin event on the client. And yes, it is the teamcolor of admins.

Then @EmbatTheHybrid is right, that can be exploited. Store the values on the server and just have it detect which event was fired and get the value for that.

1 Like

Even though your solution has been found, I would recommend cxhanging your code around to this

local Admins = {}

local teams = game:GetService("Teams")

game.ReplicatedStorage.Remotes.ChangeTeam.Prisoner.OnServerEvent:Connect(function(player)
	player.TeamColor = teams["Prisoner"].TeamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Police.OnServerEvent:Connect(function(player)
	player.TeamColor = teams["Police"].TeamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Admin.OnServerEvent:Connect(function(player)
	if table.find(Admins, player.UserId) then
		player.TeamColor = teams["Admin"].TeamColor
		wait(0.01)
		player:LoadCharacter()
	end	
end)

This ensures that exploiters can’t exploit the teams by changing giving the color of the Admin Team, just change the team names around for your team names, never trust the Client

2 Likes

Hm. Even with my player ID in the table, it doesn’t change my team now. Works for other teams though.

Hmm, maybe revert it to how it was before with looping through the table?

local Admins = {}

local teams = game:GetService("Teams")

game.ReplicatedStorage.Remotes.ChangeTeam.Prisoner.OnServerEvent:Connect(function(player)
	player.TeamColor = teams["Prisoner"].TeamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Police.OnServerEvent:Connect(function(player)
	player.TeamColor = teams["Police"].TeamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Admin.OnServerEvent:Connect(function(player, teamColor)
	for _,id in pairs(Admins) do
		if player.UserId == id then
			player.TeamColor = teams["Admin"].TeamColor
			wait(0.01)
			player:LoadCharacter()
		end
	end	
end)
1 Like

Nope, still nothing. Nothing in the console, nothing at all.

Hmm, print your userid and the ids it finds in the table, maybe they’re not the same?

print(player.UserId)
for _,id in pairs(Admins) do
    print(id)

Prints are both the same, just doesn’t change the team.

Did you remember to change "Admin" to the name of your admin team? If they’re the same, then it could be an incorrect name. Or just to be safe, add a print inside of the if statement to ensure it’s getting through the event

Team names are both the same.

Current code:

local Admins = {"1636313428"}

local teams = game:GetService("Teams")

game.ReplicatedStorage.Remotes.ChangeTeam.Prisoner.OnServerEvent:Connect(function(player)
	player.TeamColor = teams["Prisoner"].TeamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Police.OnServerEvent:Connect(function(player)
	player.TeamColor = teams["Police"].TeamColor
	wait(0.01)
	player:LoadCharacter()
end)

game.ReplicatedStorage.Remotes.ChangeTeam.Admin.OnServerEvent:Connect(function(player, teamColor)
	print(player.UserId)
	for _,id in pairs(Admins) do
		print(id)
		if player.UserId == id then
			player.TeamColor = teams["Admin"].TeamColor
			wait(0.01)
			player:LoadCharacter()
		end
	end	
end)

There’s you issue, the Id in your table is a string, make it a number

local Admins = {1636313428}
1 Like