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

What is the correct way to check if the player is on a certain team?

local fakeChar = game.ServerStorage.PeasantChar -- The character to load
local respawnTime = 2.5 -- How many seconds the player remains dead before they respawn

if game.Players.LocalPlayer.TeamColor == "Brown" then
	game.Players.CharacterAutoLoads = false -- Disable default character loading
else
	game.Players.CharacterAutoLoads = true
end	

function playerAdded(player)
	if player.Character and player.Character.Parent then -- Destroy an already-existing character
		player.Character:Destroy()
	end

	while player.Parent do -- Repeat until they leave the game
		loadChar(player) -- Method waits until they die
		wait(respawnTime)
	end
end

function loadChar(player)
	local char = fakeChar:clone()
	char.Name = player.Name

	local humanoid
	for i, child in pairs(char:GetChildren()) do -- Find the humanoid
		if child.ClassName == "Humanoid" then
			humanoid = child
			break
		end
	end

	if not humanoid then -- If no humanoid was found, make one
		humanoid = Instance.new("Humanoid", char)
	end

	player.Character = char
	char.Parent = game.Workspace
	humanoid.Died:wait() -- Wait until they die
end

game.Players.PlayerAdded:connect(playerAdded)

for i, player in pairs(game.Players:GetPlayers()) do -- Play Solo support
	if player.Character then
		player.Character:Destroy()
	end

	playerAdded(player)
end

I am trying to make it so that unless you are on the teamcolor brown, your character does not auto load, but I am getting an error on line 4:" ServerScriptService.CharLoader:4: attempt to index nil with 'TeamColor' " Could someone please explain the correct way to check if the player is on a certain team?

2 comments

0

is this a script or local script?

0

serverscript

1 answer

✓ Accepted answer

Quick answer:

  • Server storage can only be acessed by a server script type.

  • LocalPlayer can only be acessed by a local script type.

  • You dont need to iterate with a loop to find Humanoid. If you cant call by name, use Instance:FindFirstChildWhichIsA("Humanoid") or instance:FindFirstChildOfClass("Humanoid") (actually i dont know the difference between these two, but dev hub has infos if u need)

  • Instead of TeamColor == "Brown", do TeamColor == BrickColor.new("Brown"), You can't compare a BrickColor info with a String info (unless you use BrickColor.Name).

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