Why does this script do the things it's not supposed to do? team assigning

Hello, i want to make a hide & seek game when if a server has 2 or more players the game starts and a random player gets chosen to be the seeker. I did make it so when a player joins they instantly get assigned to the spectator team. but when the player joins they get put into the seeker team?
here is my code:

task.wait(1)
game.Players.PlayerAdded:Connect(function(plr)
	plr = 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 = game.Teams.Seeker
		end
	end
	
	
	
	if Seek == game.Teams.Spectator then
		MakeSeeker()
		--Seek.HumanoidRootPart.Position = Vector3.new(130.046, 5.5, 0.84)
		--Seek = game.Teams.Seeker
	else
		if playerCharacters == game.Teams.Seeker then
			print("this guy is murderer!")
			return nil
		else
			
			playerCharacters = game.Teams.Hiders
		end
	end

	end


if you know why this is not working please help.

I think you are trying to assign the player itself as the team “spectator” instead of the player’s team. you can simply do “plr.Team = game.Teams.Spectator”. Also you are trying to assign a team to a table “playerCharacters = game.Teams.Hiders”

task.wait(1)
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
1 Like

for me it wont work. i checked the output no errors, but i still get put into seeker team without me wanting it.

Is that the full script? and why is there a task.wait(1) on the first line?

1 Like

to let the character load into the game, sometimes a player needs more time to join the game fully and if you dont wait before the player is loaded in the script can bug. thats what i have been told.

I think you will have to rewrite most of the script. This script won’t work because it’s not getting the signal if recol changed and the first person who joined the server won’t fire the .PlayerAdded function as you added a task.wait(1).

1 Like

I deleted the wait as you said and it works perfect!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.