Hey everyone, I’ve been having some issues with my code when it comes to 15+ people within a server. I’ve tried to before but have failed to do so unfortunately.
How can I optimize this so the code can work for 15-80+ in a server?
Thank you in advanced!
The code is a loop, which is used to create matches within my game
while true do wait(2.5)
local AllPlayers = {}
for i,v in pairs(game.Players:GetPlayers()) do
--print(v)
table.insert(AllPlayers, #AllPlayers + 1, v)
end
if enufPlayersToStart <= #AllPlayers then
local CouldFight = {}
local Player1
local Player2
for i,v in pairs(game.Players:GetPlayers()) do
if v.Information.CurrentlyAFK.Value == false then
if v.Information.InMatch.Value == false then
if v.Information.Died.Value == false then
if v.Character then
table.insert(CouldFight, #CouldFight + 1, v)
end
end
end
end
end
if #CouldFight >= 2 then
local Picker = math.random(1, #CouldFight) --1
local Picker2 = math.random(1, #CouldFight)--3
Player1 = CouldFight[Picker]--1
Player2 = CouldFight[Picker2]--3
if Player1 == Player2 then
repeat wait()
local Picker = math.random(1, #CouldFight)
local Picker2 = math.random(1, #CouldFight)
Player1 = CouldFight[Picker]
Player2 = CouldFight[Picker2]
until
Player1 ~= Player2
end
end
if game.Players:FindFirstChild(tostring(Player1)) and game.Players:FindFirstChild(tostring(Player2)) then
local Arenaz = nil
local AvArenas = {}
for i,v in pairs(game.ServerStorage.Arenas:GetChildren()) do
if v.Active.Value == false then
table.insert(AvArenas, #AvArenas + 1, v.Name)
end
end
print(#AvArenas)
if #AvArenas >= 1 then
Arenaz = AvArenas[math.random(1, #AvArenas)]
game.ServerStorage.Arenas[Arenaz].Active.Value = true
local Mapz = game.ServerStorage.Maps:GetChildren()
local PickedMap = Mapz[math.random(1, #Mapz)]:Clone()
PickedMap.Parent = workspace
PickedMap:MoveTo(workspace:FindFirstChild(tostring(Arenaz)).Core.Position)
PickedMap.Parent = workspace:FindFirstChild(tostring(Arenaz))
local cframe = PickedMap.PrimaryPart.CFrame -- Current CFrame
local rotation = workspace:FindFirstChild(tostring(Arenaz)).Core.Orientation.Y
PickedMap:SetPrimaryPartCFrame(cframe * CFrame.Angles(0, math.rad(rotation), 0))
local SetPlayerStats = coroutine.create(function()
game.Players[tostring(Player1)]:WaitForChild("Information").InMatch.Value = true
game.Players[tostring(Player2)]:WaitForChild("Information").InMatch.Value = true
local Player1 = Player1
local Player2 = Player2
local Arenaz = Arenaz
local PickedMap = PickedMap
wait(2)
if game.Players:FindFirstChild(tostring(Player1)) and game.Players:FindFirstChild(tostring(Player2)) then
if game.Players[tostring(Player1)]:WaitForChild("Information").CurrentSword.Value == "" then
game.ServerStorage.SwordModels.Linked:Clone().Parent = game.Players[tostring(Player1)].Information.Inventory
game.Players[tostring(Player1)].Information.CurrentSword.Value = "Linked"
end
if game.Players[tostring(Player2)]:WaitForChild("Information").CurrentSword.Value == "" then
game.ServerStorage.SwordModels.Linked:Clone().Parent = game.Players[tostring(Player2)].Information.Inventory
game.Players[tostring(Player2)].Information.CurrentSword.Value = "Linked"
end
if not game.Players[tostring(Player1)]:WaitForChild("Backpack"):FindFirstChildWhichIsA("Tool") then
local Sword = game.Players[tostring(Player1)].Information.Inventory:WaitForChild(game.Players[tostring(Player1)].Information.CurrentSword.Value, 3):Clone()
if Sword then
Sword.Parent = game.Players[tostring(Player1)]:WaitForChild("Backpack")
end
end
if not game.Players[tostring(Player2)]:WaitForChild("Backpack"):FindFirstChildWhichIsA("Tool") then
local Sword = game.Players[tostring(Player2)].Information.Inventory:WaitForChild(game.Players[tostring(Player2)].Information.CurrentSword.Value, 3):Clone()
if Sword then
Sword.Parent = game.Players[tostring(Player2)]:WaitForChild("Backpack")
end
end
game.Players[tostring(Player1)]:WaitForChild("Information").Hostile.Value = Player2
game.Players[tostring(Player2)]:WaitForChild("Information").Hostile.Value = Player1
game.Players[tostring(Player1)].Character.Humanoid.Health = 100
game.Players[tostring(Player2)].Character.Humanoid.Health = 100
game.Players[tostring(Player1)]:WaitForChild("Information").Arena.Value = Arenaz
game.Players[tostring(Player2)]:WaitForChild("Information").Arena.Value = Arenaz
game.Players[tostring(Player1)]:WaitForChild("Information").MapUsed.Value = tostring(PickedMap)
game.Players[tostring(Player2)]:WaitForChild("Information").MapUsed.Value = tostring(PickedMap)
game.Players[tostring(Player1)].Character:FindFirstChild("Humanoid").WalkSpeed = 0
game.Players[tostring(Player2)].Character:FindFirstChild("Humanoid").WalkSpeed = 0
game.Players[tostring(Player1)].Character:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace[Arenaz]:FindFirstChild(tostring(PickedMap)).Team1Spawn.CFrame
game.Players[tostring(Player2)].Character:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace[Arenaz]:FindFirstChild(tostring(PickedMap)).Team2Spawn.CFrame
Events.MatchInfo:FireClient(game.Players[tostring(Player1)], tostring(Player2), tostring(PickedMap))
Events.MatchInfo:FireClient(game.Players[tostring(Player2)], tostring(Player1), tostring(PickedMap))
local p1 = game.Players:FindFirstChild(tostring(Player1))
local p2 = game.Players:FindFirstChild(tostring(Player2))
if p1 then
p1.Information.PlayerCountdown.Value = "3"
end
if p2 then
p2.Information.PlayerCountdown.Value = "3"
end
wait(1)
if p1 then
p1.Information.PlayerCountdown.Value = "2"
end
if p2 then
p2.Information.PlayerCountdown.Value = "2"
end
wait(1)
if p1 then
p1.Information.PlayerCountdown.Value = "1"
end
if p2 then
p2.Information.PlayerCountdown.Value = "1"
end
wait(1)
if p1 then
p1.Information.PlayerCountdown.Value = "BEGIN"
end
if p2 then
p2.Information.PlayerCountdown.Value = "BEGIN"
end
local PlayerA = tostring(Player1)
local PlayerB = tostring(Player2)
if game.Players:FindFirstChild(PlayerA) then
if game.Players:FindFirstChild(PlayerA).Character then
game.Players:FindFirstChild(PlayerA).Character.Humanoid.WalkSpeed = 16
end
end
if game.Players:FindFirstChild(PlayerB) then
if game.Players:FindFirstChild(PlayerB).Character then
game.Players:FindFirstChild(PlayerB).Character.Humanoid.WalkSpeed = 16
end
end
table.insert(PlayersTableStart.Players, #PlayersTableStart.Players + 1, tostring(Player1))
table.insert(PlayersTableStart.Players, #PlayersTableStart.Players + 1, tostring(Player2))
else
game.ServerStorage.Arenas[Arenaz].Active.Value = false
PickedMap:Destroy()
end
end)
coroutine.resume(SetPlayerStats)
end
end
end
end