Why is this team assigning script not doing stuff properly?

Hello, im trying to make a game of hide and seek, when a player joins they get assigned to a team called “Spectator”. when the game has more than 2 players the round starts. the game should pick a random player and make them the seeker. and the rest the hider. the problem is nothing happens. why?

game.Players.PlayerAdded:Connect(function(plr)
	plr.Team = game.Teams.Spectator
end)

local recol = 0


game.Players.PlayerAdded:Connect(function(amount)
	recol = recol + 1
	print("Added 1")
end)

game.Players.PlayerRemoving:Connect(function(amount)
	recol = recol - 1
	print("Lost one player!")
end)

if recol <= 2 then
	print("Sorry but the game has less than 2 players!")
	return nil
end

if recol >= 2 then 
	wait(20)
	
	local playerCharacters = {}

	for _, character in pairs(workspace:GetChildren()) do
		if game.Players:FindFirstChild(character.Name) then
			table.insert(playerCharacters, character)
		end
	end
	
	
	local Seek = playerCharacters[math.random(1, #playerCharacters)]
	print(Seek)
	
	function MakeSeeker()
		if recol >= 2 then
			Seek.Team = game.Teams.Seeker
		end
	end
	
	
	
	if Seek.Team == game.Teams.Spectator then
		MakeSeeker()
		--Seek.HumanoidRootPart.Position = Vector3.new(130.046, 5.5, 0.84)
		--Seek.Team = game.Teams.Seeker
	else
		if Seek.Team == game.Teams.Seeker then
			print("this guy is murderer!")
			return nil
		else
			
			Seek.Team = game.Teams.Hiders
		end
	end

	end
3 Likes

You have two PlayerAdded events. May I ask why?

(Maybe the second one is firing and it ain’t assigning the player to a team).

From what I’ve seen, I assume that every time a player respawns you wanna give em some value, if so, you should use CharacterAdded.

Also, you’re only checking if “recol” is bigger or equal to 2 only ONCE. You should probably check multiple times / over and over again.

no, i want to add a server value where if a player joins it does + 1 and if a player leaves it does - 1. so the game knows how much players there are currently and will start the round if it has 2 or more.

The first PlayerAdded event.

The second one.

I’d recommend doing this instead (and get rid of the two old ones):

game.Players.PlayerAdded:Connect(function(amount)
	recol = recol + 1
        plr.Team = game.Teams.Spectator
	print("Added 1")
end)

ill give that a try now. i will let you know if it works or not.

1 Like

You could turn this:

To this:

task.spawn(function()
while task.wait(1) do
if recol >= 2 then 
	task.wait(20)
	
	local playerCharacters = {}

	for _, character in pairs(workspace:GetChildren()) do
		if game.Players:FindFirstChild(character.Name) then
			table.insert(playerCharacters, character)
		end
	end
	
	
	local Seek = playerCharacters[math.random(1, #playerCharacters)]
	print(Seek)
	
	local function MakeSeeker()
		if recol >= 2 then
			Seek.Team = game.Teams.Seeker
		end
	end
	
	
	
	if Seek.Team == game.Teams.Spectator then
		MakeSeeker()
		--Seek.HumanoidRootPart.Position = Vector3.new(130.046, 5.5, 0.84)
		--Seek.Team = game.Teams.Seeker
	else
		if Seek.Team == game.Teams.Seeker then
			print("this guy is murderer!")
			return nil
		else
			
			Seek.Team = game.Teams.Hiders
		end
	end

	end
end
end)

If you don’t know what I did, I spawned a new thread and added a while wait loop that checks every second to see if “recol” is more than or equal to 2.

Here’s the full script so far:

game.Players.PlayerAdded:Connect(function(amount)
	recol += 1
        plr.Team = game.Teams.Spectator
	print("Added a player!")
end)

task.spawn(function()
repeat task.wait(1) until recol >= 2
	task.wait(20)
	local playerCharacters = {}
	for _, character in pairs(workspace:GetChildren()) do
		if game.Players:FindFirstChild(character.Name) then
			table.insert(playerCharacters, character)
		end
	end
	
	local Seek = playerCharacters[math.random(1, #playerCharacters)]
	print(Seek)
	
	local function MakeSeeker()
		if recol >= 2 then
			Seek.Team = game.Teams.Seeker
		end
	end
	
	if Seek.Team == game.Teams.Spectator then
		MakeSeeker()
		--Seek.HumanoidRootPart.Position = Vector3.new(130.046, 5.5, 0.84)
		--Seek.Team = game.Teams.Seeker
	else
		if Seek.Team == game.Teams.Seeker then
			print("this guy is murderer!")
			return nil
		else
			Seek.Team = game.Teams.Hiders
		end
	end
end)

P.S. I changed the while loop to a repeat until loop (repeat task.wait(1) until recol >= 2)

1 Like

image
im getting this error

image
its on this line, seek.team is the problem but if the plr.team works fine then why this wont?

Ah, it’s because you’re attempting to get the player’s team from their character model. Do this:

if game.Players:GetPlayerFromCharacter(Seek).Team == game.Teams.Spectator then
1 Like

im still getting the same error. line 28

do i do the same for that line?
image

1 Like

now it does every 20 seconds it just takes a player of spectator and puts it into seeker.
image

will this work?
image

1 Like

Possibly, I’m not sure. Also sorry for the late response :sweat_smile:

no worries :laughing:. i revamped the script a little bit at the bottom. you should check it out.

local recol = 0

game.Players.PlayerAdded:Connect(function(plr)
	recol = recol + 1
	plr.Team = game.Teams.Spectator
	print("Added 1")
end)

task.spawn(function()
	while task.wait(1) do
		if recol >= 2 then 
			task.wait(20)
			

			local playerCharacters = {}

			for _, character in pairs(workspace:GetChildren()) do
				if game.Players:FindFirstChild(character.Name) then
					table.insert(playerCharacters, character)
				end
			end


			local Seek = playerCharacters[math.random(1, #playerCharacters)]
			print(Seek)

			local function MakeSeeker()
				if recol >= 2 then
					game.Players:GetPlayerFromCharacter(Seek).Team = game.Teams.Seeker
				end
			end



			if game.Players:GetPlayerFromCharacter(Seek).Team == game.Teams.Spectator then
				MakeSeeker()
				--Seek.HumanoidRootPart.Position = Vector3.new(130.046, 5.5, 0.84)
				--Seek.Team = game.Teams.Seeker
			else
				if Seek.Team == game.Teams.Seeker then
					print("this guy is murderer!")
					return nil
				else

					
				
					
				end
				task.wait(5)
				game.Players:GetPlayerFromCharacter(playerCharacters).Team = game.Teams.Spectator
				
			end
			while wait(0.1) do
				local bag = playerCharacters[math.random(1, #playerCharacters)]
				if game.Players:GetPlayerFromCharacter(bag).Team == game.Teams.Spectator then
					game.Players:GetPlayerFromCharacter(bag).Team = game.Teams.Hiders
				end
				
			end

		end
	end
end)

this seems to work. but i do see that at the while wait(0.1) that now its infinitly looping there, not repeating the whole script.

You should wrap it in task.spawn() (creating a new thread so other code can run)

task.spawn(function()
while wait(blah) do
end
end)

so something like

task.spawn(functions()
       while wait(0.1) do
				local bag = playerCharacters[math.random(1, #playerCharacters)]
				if game.Players:GetPlayerFromCharacter(bag).Team == game.Teams.Spectator then
					game.Players:GetPlayerFromCharacter(bag).Team = game.Teams.Hiders
				end
				
			end

yeah but function without the “s” (unless that was intentional).

Also, you need to add a parenthesis “)” next to the end

1 Like