repeat
randomSpawn = tps[math.random(1, #tps)]
until not spawnedPlayers[randomSpawn]
spawnedPlayers[randomSpawn] = true
hrp.CFrame = randomSpawn.CFrame
print(#playersinGame)
end
end
this is giving me a timeout exhaustion error how do I fix it?
When using repeat / until
loops, they act similar to while true
loops, they have no delay when running the next loop. Just add a task.wait()
inside of the repeat
loop.
alright i added this and it works… but now there is a new problem with my script. its a 1v1 round system and when there is 2 people in the game its fine but when there is 3 it breaks. Heres my code for adding the peeps and removing the peeps
local tps = pickedMap.Spawns:GetChildren()
local spawnedPlayers = {}
for _, player in pairs(PlayersService:GetPlayers()) do
local character = player.Character
if character and #spawnedPlayers < 2 then
table.insert(playersinGame,player)
print(playersinGame)
local hrp = character.HumanoidRootPart
local randomSpawn
local classicSword = game.ServerStorage.ClassicSword
local flowerSword = game.ServerStorage.FlowerSword
if character.Sword.Value == 1 then
local swordClone = classicSword:Clone()
swordClone.Parent = player.Backpack
end
if character.Sword.Value == 2 then
local swordClone = flowerSword:Clone()
swordClone.Parent = player.Backpack
end
repeat wait()
randomSpawn = tps[math.random(1, #tps)]
until not spawnedPlayers[randomSpawn]
spawnedPlayers[randomSpawn] = true
hrp.CFrame = randomSpawn.CFrame
print(#playersinGame)
end
end
local winner = nil
for i = gameTime, 0, -1 do
if #playersinGame <= 1 then
if #playersinGame == 1 then
winner = playersinGame[1]
status.Value = "gameover! winner: ".. winner.DisplayName
winner.leaderstats["wins 🏆"].Value = winner.leaderstats["wins 🏆"].Value + 1
victory:Play()
wait(2)
end
break
else
if i < 6 then
status.Value = "Fight >:D : " .. i
gameTick:Play()
else
status.Value = "Fight >:D : " .. i
end
task.wait(1)
status.Value = "nobody won :("
end
end
for i, player in pairs(PlayersService:GetPlayers()) do
local character = player.Character
character.Humanoid.Health = character.Humanoid.MaxHealth
local toolbackpack = player.Backpack:FindFirstChildWhichIsA("Tool")
if toolbackpack then
toolbackpack:Destroy()
end
local charactertool = character:FindFirstChildWhichIsA("Tool")
if charactertool then
charactertool:Destroy()
end
if character and character:FindFirstChild("HumanoidRootPart") then
local hrp = character.HumanoidRootPart
hrp.CFrame = lobby.Spawn.CFrame
end
end
musictransition(fightMusic,intermissionMusic)
playersinGame = {}
clonedMap:Destroy()
end
Maybe use #playersInGame
instead of #spawnedPlayers
?
Also if you are trying to pick random players, use math.random
and pick from a table of players. I tried editing your code to do something like that:
local playerList = game.Players:GetChildren()
local spawnedPlayers = {}
repeat
local randomNumber = math.random(1, #playerList)
if playerList[randomNumber].Character ~= nil and spawnedPlayers[playerList[randomNumber]] == nil then
table.insert(spawnedPlayers[playerList[randomNumber]]
--Put what else you need to do when spawning a player
end
until #spawnedPlayers == 2
it still breaks unfortunately… it just stays on the status go and the whole script breaks