RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"fix this function to work well and optimize it and make sure no errors will happen"
local function startRound() if #players:GetPlayers() < minPlayers then print("Not enough players to start a round.") return end print("Starting round...") local alivePlayers = {} for _, player in pairs(players:GetPlayers()) do if player.Character and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health > 0 then table.insert(alivePlayers, player) end end if #alivePlayers == 0 then print("No alive players to start a round.") return end ballHolder = alivePlayers[math.random(#alivePlayers)] playerStates[ballHolder.Name] = {HasBall = true, IsAlive = true} for _, player in pairs(alivePlayers) do local character = player.Character if character then local randomPos repeat local randomSpawn = spawnLocations[math.random(#spawnLocations)] -- get a random spawn location randomPos = randomSpawn.Position -- get the position of the spawn location until (randomPos - ballStartPos).Magnitude > ball.Size.Magnitude * 2 -- make sure the player is not too close to the ball character.HumanoidRootPart.Position = randomPos table.remove(spawnLocations, table.find(spawnLocations, randomSpawn)) end end end -- Event to handle when a player fires the remote event game.ReplicatedStorage.LaunchBall.OnServerEvent:Connect(function(plr) if plr == ballHolder and playerStates[plr.Name].HasBall then local alivePlayers = {} for _, player in pairs(players:GetPlayers()) do if playerStates[player.Name].IsAlive then table.insert(alivePlayers, player) end end if #alivePlayers == 0 then print("No alive players to pass the ball.") return end ballHolder = alivePlayers[math.random(#alivePlayers)] playerStates[ballHolder.Name] = {HasBall = true, IsAlive = true} ball.Velocity = (ballHolder.Character.HumanoidRootPart.Position - ball.Position).unit * 30 end end)