local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Sword_Fight_Arena = workspace:WaitForChild("Sword_Fight_One")
local Arena_Values = Sword_Fight_Arena:WaitForChild("Arena_Player_Values")
local Player_One_ID = Arena_Values:WaitForChild("Player_One_ID")
local Player_Two_ID = Arena_Values:WaitForChild("Player_Two_ID")
local Players_In_Arena = Arena_Values:WaitForChild("Players_In_Arena")
local Join_Parts = Sword_Fight_Arena:WaitForChild("Join_Parts")
local Join_Part_Player_One = Join_Parts:WaitForChild("Join_Player_One")
local Join_Part_Player_Two = Join_Parts:WaitForChild("Join_Player_Two")
local Player_One_Prompt = Join_Part_Player_One.Attachment:WaitForChild("Player_One_Prompt")
local Player_Two_Prompt = Join_Part_Player_Two.Attachment:WaitForChild("Player_Two_Prompt")
local Lobbies = workspace:WaitForChild("Lobbies")
local Secondary_Lobby = Lobbies:WaitForChild("Secondary_Lobby")
local Lobby = Secondary_Lobby:WaitForChild("lobby")
local Secondary_Spawn = Lobby:WaitForChild("Secondary_Spawn")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Arena_Modules = Modules:WaitForChild("Arena_Modules")
local Arena_Functions = require(Arena_Modules:WaitForChild("Arena_Functions"))
local Player_One_Color = Color3.fromRGB(255, 0, 0)
local Player_Two_Color = Color3.fromRGB(0, 100, 255)
Players.PlayerAdded:Connect(function(player)
local Player_ID = player.UserId
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Died:Once(function()
if Player_ID == Player_One_ID.Value then
Player_One_ID.Value = 0
Players_In_Arena.Value = 0
if Player_Two_ID.Value == 0 then
print("Player One resetted and there was no one in Player Two.")
Arena_Functions.Toggle_Join_Prompt_and_Part(Join_Part_Player_One, Player_One_Prompt, true, Player_One_Color)
-- This module function just tweens the join part color back to normal and enables the Player Prompt
return
end
local Opponent = Players:GetPlayerByUserId(Player_Two_ID.Value)
local Opponent_Character = Opponent.Character
Arena_Functions.Create_ForceField(Opponent_Character)
-- Creates forcefield around the opponent's character so they don't get spawn killed when they are teleported back
Arena_Functions.Award_Winner_Leaderstats(Players:GetPlayerByUserId(Opponent))
-- Increases the Opponent's leaderstats
Player_Two_ID.Value = 0
Opponent_Character:PivotTo(CFrame.new(Secondary_Spawn.Position + Vector3.new(0, 5, 0)))
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_One, Player_One_Prompt, true, Player_One_Color)
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_Two, Player_Two_Prompt, true, Player_Two_Color)
elseif Player_ID == Player_Two_ID.Value then
Player_Two_ID.Value = 0
Players_In_Arena.Value = 0
if Player_One_ID.Value == 0 then
print("Player Two resetted and there was no one in Player One.")
Arena_Functions.Toggle_Join_Prompt_and_Part(Join_Part_Player_Two, Player_Two_Prompt, true, Player_Two_Color)
return
end
local Opponent = Players:GetPlayerByUserId(Player_One_ID.Value)
local Opponent_Character = Opponent.Character
Arena_Functions.Create_ForceField(Opponent_Character)
Arena_Functions.Award_Winner_Leaderstats(Players:GetPlayerByUserId(Opponent))
Opponent_Character:PivotTo(CFrame.new(Secondary_Spawn.Position + Vector3.new(0, 5, 0)))
Player_One_ID.Value = 0
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_One, Player_One_Prompt, true, Player_One_Color)
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_Two, Player_Two_Prompt, true, Player_Two_Color)
end
end)
end)
end)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Sword_Fight_Arena = workspace:WaitForChild("Sword_Fight_One")
local Arena_Values = Sword_Fight_Arena:WaitForChild("Arena_Player_Values")
local Player_One_ID = Arena_Values:WaitForChild("Player_One_ID")
local Player_Two_ID = Arena_Values:WaitForChild("Player_Two_ID")
local Players_In_Arena = Arena_Values:WaitForChild("Players_In_Arena")
local Join_Parts = Sword_Fight_Arena:WaitForChild("Join_Parts")
local Join_Part_Player_One = Join_Parts:WaitForChild("Join_Player_One")
local Join_Part_Player_Two = Join_Parts:WaitForChild("Join_Player_Two")
local Player_One_Prompt = Join_Part_Player_One.Attachment:WaitForChild("Player_One_Prompt")
local Player_Two_Prompt = Join_Part_Player_Two.Attachment:WaitForChild("Player_Two_Prompt")
local Lobbies = workspace:WaitForChild("Lobbies")
local Secondary_Lobby = Lobbies:WaitForChild("Secondary_Lobby")
local Lobby = Secondary_Lobby:WaitForChild("lobby")
local Secondary_Spawn = Lobby:WaitForChild("Secondary_Spawn")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Arena_Modules = Modules:WaitForChild("Arena_Modules")
local Arena_Functions = require(Arena_Modules:WaitForChild("Arena_Functions"))
local Player_One_Color = Color3.fromRGB(255, 0, 0)
local Player_Two_Color = Color3.fromRGB(0, 100, 255)
Players.PlayerRemoving:Connect(function(player)
local Player_ID = player.UserId
if Player_ID == Player_One_ID.Value then
Player_One_ID.Value = 0
Players_In_Arena.Value = 0
if Player_Two_ID.Value == 0 then
print("Player One left and there is no one in Player Two.")
Arena_Functions.Toggle_Join_Prompt_and_Part(Join_Part_Player_One, Player_One_Prompt, true, Player_One_Color)
return
end
local Opponent = Players:GetPlayerByUserId(Player_Two_ID.Value)
local Opponent_Character = Opponent.Character
Arena_Functions.Create_ForceField(Opponent_Character)
Arena_Functions.Award_Winner_Leaderstats(Opponent)
Opponent_Character:PivotTo(CFrame.new(Secondary_Spawn.Position + Vector3.new(0, 5, 0)))
Player_Two_ID.Value = 0
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_One, Player_One_Prompt, true, Player_One_Color)
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_Two, Player_Two_Prompt, true, Player_Two_Color)
elseif Player_ID == Player_Two_ID.Value then
Player_Two_ID.Value = 0
Players_In_Arena.Value = 0
if Player_One_ID.Value == 0 then
print("Player Two left and there is no one in Player One.")
Arena_Functions.Toggle_Join_Prompt_and_Part(Join_Part_Player_Two, Player_Two_Prompt, true, Player_Two_Color)
return
end
local Opponent = Players:GetPlayerByUserId(Player_One_ID.Value)
local Opponent_Character = Opponent.Character
Arena_Functions.Create_ForceField(Opponent_Character)
Arena_Functions.Award_Winner_Leaderstats(Opponent)
Opponent_Character:PivotTo(CFrame.new(Secondary_Spawn.Position + Vector3.new(0, 5, 0)))
Player_Two_ID.Value = 0
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_One, Player_One_Prompt, true, Player_One_Color)
task.spawn(Arena_Functions.Toggle_Join_Prompt_and_Part, Join_Part_Player_Two, Player_Two_Prompt, true, Player_Two_Color)
end
end)
Hello! I’m making a sword fighting arena where two players will enter and fight. I made two scripts that handles if a player dies and checks if they are a fighter in the arena and when they leave as well. However, I think that my code is unoptimized and unefficient because it is repetitve. I’m trying to use module scripts to reduce repeating my code over and over but I don’t know how since it still looks the same. Any suggestions to improve my code? Also at some point, I will want multiple arenas and I don’t want to keep cloning the same script over and over again.

