How do I set the players team based on a NumberValue in the character's head?

I have been trying to make randomized preset StarterCharacters and make only the intended StarterCharacter be assigned to their own team (e.g. Char1 contains the NumberValue of 8 in it’s head, making the second script send it into the White team.)

The problem is that I have no idea what is going wrong in my script and I feel that I am doing something fundamentally wrong. I have searched countless websites trying to figure out what is wrong with my code and didn’t find anything wrong with it but it still doesn’t do anything.

	local players = game:GetService("Players")
	local Teams11 = game:GetService("Teams")
	local teamWhite = Teams11.White
	local teamBlack = Teams11.Black
	local teamNeil = Teams11.Neil

while wait (.5) do

if players.Character.Head.CharNumber == 1 then
	local function play(player)
		player.Team = teamWhite
	end 


if players.Character.Head.CharNumber == 2 then
	local function play(player)
		player.Team = teamBlack
	end 
end

if players.Character.Head.CharNumber == 10 then
	local function play(player)
		player.Team = teamNeil
	end 
end

end

1 Like

From what I can tell your script should work as intended by setting the Player team depending on the number. Is this not the case, or is it another aspect you’re looking for help with such as the actual changing the player into the preset character?

Edit: Whoops, didn’t really look over it much because I wanted to confirm what the problem was. Fix posted below

1 Like

I don’t have any problems changing the player into the preset character. The only problem I have been having is putting the character into one of the 3 teams I already have by checking the number. When I playtest the game, it does everything right except putting the player into the team.

1 Like

Are you running your code in a Script or LocalScript?

I am running my code in a script.

Why are you wrapping the code for each individual change to the Players team into a function? You could do that if needed, however you’ll need to call the function after. I’d try formatting your code like this instead:

local function InterpretTeam(Player)
    local Num = Player.Character.Head.CharNumber.Value 
    if Num == 1 then
        Player.Team = teamWhite
    end 

    if Num == 2 then
        Player.Team = teamWhite
    end
    --Continue for other teams
end

Then call InterpretTeam(PlayerToAssign) when needed.

It didn’t work for me. I’ll upload a video of what’s going on in game.

The way you have it right now the function isn’t supplied a Player as the argument when you call it. If you’re wanting to call this for all players, do something like this:

for _, v in pairs(players:GetChildren()) do
    InterpretTeam(v)
end

Where would I put this in the script? I haven’t learned for i, v things yet…

Somehwere after the function itself, if you’re wanting to wait .5 and then check like you were before you can do so. The loop runs through all Children of game.Players assigning the current player the value “v” in each iteration., therefore when you call it inside the loop providing v as the argument for Player it’ll run for every player in game.

Would this work?

local function InterpretTeam(Player)
local Num = Player.Character.Head.CharNumber.Value
if Num == 1 then
players.Team = teamWhite
end

if Num == 2 then
	players.Team = teamBlack
end
--Continue for other teams
if Num == 10 then
	players.Team = teamNeil
end

end

for _, v in pairs(players:GetChildren()) do
InterpretTeam(v)
end

InterpretTeam()

Looks like it should to me other than the Final line of InterpretTeam(), that one’s still provided no argument for Player. Since you’ve ran through every player in the for loop calling it there again is unnecessary though. You may want to connect a game.Players.PlayerJoined(Player) to call InterpretTeam(Player) eventually as well, but that depends on your game and I’ll leave that up to your discretion.

The script didn’t work for some reason.

There were a few issues I found with this script the first one being in the InterpretTeam function when you were setting the Players team you were actually using the PlayersService variable you had created, second issue was that you were only running this once when the server immediately started at this point player’s characters have not loaded and I doubt you will have gotten the CharNumber variable into the head so I have created a loop that will check the value of the players team number every one second, here is code that works for me: (The team names don’t mean anything they’re just placeholders for the teams you will use)

local players = game:GetService("Players")

local teamWhite = game.Teams:WaitForChild("White")
local teamBlack = game.Teams:WaitForChild("Black")
local teamNeil = game.Teams:WaitForChild("Neil")

local function InterpretTeam(Player)
	local Num = Player.Character.Head.CharNumber.Value
	if Num == 1 then
		Player.Team = teamWhite
	end

	if Num == 2 then
		Player.Team = teamBlack
	end
	--Continue for other teams
	if Num == 10 then
		Player.Team = teamNeil
	end
end

game.Players.PlayerAdded:Connect(function(player)
	
	local Char = player.CharacterAdded:Wait()
	
	player.CharacterAdded:Connect(function(Character)
		
		Char = Character
		
		local val = Instance.new("IntValue", Char:WaitForChild("Head"))
		val.Value = 2
		val.Name = "CharNumber"
		
	end)
	
	local val = Instance.new("IntValue", Char:WaitForChild("Head"))
	val.Value = 2
	val.Name = "CharNumber"
	
	while wait(1) do
		
		InterpretTeam(player)
		
	end
	
end)
1 Like