Greetings dear DevForum,
I have been struggling with a sword fighting system. Let me explain to you how it is supposed to work. Basically there is this UI:
The pads detect the players properly and spawn them properly. Unfortunately there are two issues, which I cannot seem to resolve. Those are:
- The Player gets the sword too many times and spawned in too often.
- The round ends without anyone winning and only one of the two players gets reset.
I have tried adding the wait(5)
so the system waits until both players have loaded in properly, but that did not seem to help.
Here is a snippet from the script:
--##MAIN SYSTEM##--
local Player1 = nil
local Player2 = nil
local Match = false
local SpawnPart1 = game.Workspace.Spawn1
local SpawnPart2 = game.Workspace.Spawn2
local function RespawnPlayer1(Player)
Player.Character.HumanoidRootPart.CFrame = Respawn1.CFrame
if Player1 == Player then
Player1 = nil
elseif Player2 == Player then
Player2 = nil
end
end
local function ResetQue1()
if Player1 == nil then
if Player2 ~= nil then
RespawnPlayer1(Player2)
end
else
RespawnPlayer1(Player1)
end
GUI1.QueUI.Waiting.Plr1.Image = ""
GUI1.QueUI.Waiting.Plr2.Image = ""
GUI1.QueUI.Waiting.QueText.Text = "Waiting for Players...\n0/2"
end
local function SetPlayer1(PlayerFromCharacter)
Player1 = PlayerFromCharacter
local content, isready = game.Players:GetUserThumbnailAsync(PlayerFromCharacter.UserId, thumbType, thumbSize)
GUI1.QueUI.Waiting.Plr1.Image = content
end
local function SetPlayer2(PlayerFromCharacter)
Player2 = PlayerFromCharacter
local content, isready = game.Players:GetUserThumbnailAsync(PlayerFromCharacter.UserId, thumbType, thumbSize)
GUI1.QueUI.Waiting.Plr2.Image = content
end
local function SpawnPlayer1(Player, SpawnPart)
Player:LoadCharacter()
print("Character Loaded..")
Player.Character.Humanoid.Health = 100
print("Health recovered..")
print(Player.Character.PrimaryPart.CFrame)
Player.Character.PrimaryPart.CFrame = SpawnPart.CFrame
print("Player teleported..")
local Sword = DatabaseModule.LoadPlayerData(Player, "SwordDatabase")
print("Sword found..")
local SwordTool = game:GetService("ServerStorage"):WaitForChild(Sword)
print("Found in ServerStorage")
local SwordClone = SwordTool:Clone()
print("Sword Cloned")
SwordClone.Parent = Player.Backpack
print("Sword given to player")
end
local function StartGame1()
if Player1 ~= nil and Player2 ~= nil then
Match = true
SpawnPlayer1(Player1, SpawnPart1)
print("Player 1 spawned.")
SpawnPlayer1(Player2, SpawnPart2)
print("Player 2 spawned.")
wait(5)
game.Players:WaitForChild(tostring(Player1)).Character.Humanoid.Died:Connect(function()
GUI1.QueUI.Waiting.QueText.Text = Player1.Name.." has won the game and takes a win for "..Player1.Character.HumanoidRootPart.OverheadGUI.CountryInfo.Text
print("Player 2 won")
end)
game.Players:WaitForChild(tostring(Player2)).Character.Humanoid.Died:Connect(function()
GUI1.QueUI.Waiting.QueText.Text = Player2.Name.." has won the game and takes a win for "..Player2.Character.HumanoidRootPart.OverheadGUI.CountryInfo.Text
print("Player 2 won")
end)
wait(2)
ResetQue1()
print("Que Reset")
elseif Player1 == nil or Player2 == nil then
Match = false
Player1 = nil
Player2 = nil
end
end
local function Countdown()
if Player1 ~= nil and Player2 ~= nil then --Checks if there are 2 Players
GUI1.QueUI.Waiting.QueText.Text = "Starting match in..."
wait(1)
GUI1.QueUI.Waiting.QueText.Text = "3"
wait(1)
GUI1.QueUI.Waiting.QueText.Text = "2"
wait(1)
GUI1.QueUI.Waiting.QueText.Text = "1"
wait(1)
GUI1.QueUI.Waiting.QueText.Text = "GO!"
wait(1)
GUI1.QueUI.Waiting.QueText.Text = Player1.Name.." vs. "..Player2.Name
StartGame1()
end
end