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
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.
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.
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)
no worries . 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)
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