This code is the code for a sword fight minigame.
How the minigame works: You get teleported to an arena, and after 10 seconds, you are given a sword. You kill everyone, and you get rewarded. More than 1 person alive and time ran out, it continues to lobby.
local module = require(game.ReplicatedStorage.GlobalFunctions)
game.ServerStorage.StartMinigame.Event:Connect(function(minigame)
if minigame == "Sword Fight" then
script.Parent.Sound:Play()
local plrs = {}
for i, p in pairs(game.Players:GetPlayers()) do
local char = p.Character.HumanoidRootPart
char.Position = script.Parent.SwordFightSpawn.Position + Vector3.new(math.random(-50.314 / 2, 50.314 / 2), 0, math.random(-49.3 / 2, 49.3 / 2))
table.insert(plrs, p)
end
local newThread = coroutine.create(function()
game.Players.PlayerRemoving:Connect(function(p)
if (#plrs > 0) then
local item = module.finditem(plrs, p)
table.remove(plrs, item)
end
end)
while (#plrs > 0) do
for i, plr in pairs(plrs) do
if plr.Character.Humanoid.Health <= 0 then
plr.Backpack:ClearAllChildren()
local item = module.finditem(plrs, plr)
table.remove(plrs, item)
end
end
wait(0.5)
end
end)
coroutine.resume(newThread)
for i = 10, 1, -1 do
game.ReplicatedStorage.Text:FireAllClients("In "..tostring(i).." seconds, you will have to kill all opponents with a sword.")
wait(1)
end
for i, pl in pairs(plrs) do
local sword = game.ServerStorage.MinigameTools["1. Sword Fight Sword"]:Clone()
sword.Parent = pl.Backpack
end
local val = 60
repeat
game.ReplicatedStorage.Text:FireAllClients("You have "..tostring(val).." seconds left to kill all opponents.")
val = val - 1
wait(1)
until #plrs <= 1 or val == 0
if #plrs <= 1 then
if plrs[1] then
game.ReplicatedStorage.Text:FireAllClients(plrs[1].Name.." has defeated all opponents!")
end
if #plrs == 0 then
game.ReplicatedStorage.Text:FireAllClients("Somehow the last two contestants died at the same time, therefore no one has won.")
end
end
if val == 0 then
game.ReplicatedStorage.Text:FireAllClients("Time has ran out!")
end
for i, player in pairs(plrs) do
local item = module.finditem(plrs, player)
table.remove(plrs, item)
end
wait(3)
script.Parent.Sound:Stop()
game.ServerStorage.BeginIntermission:Fire()
wait(1)
script.Parent.Parent = game.ServerStorage.Minigames
end
end)
How do I improve, and would this work?