Any Ideas why my script isn't working?

Hi guys, i’m trying to make a script so that when a player presses the play button, two players are teleported into an arena and are given a sword. Then, they fight one another until of one them reaches 5 ‘Kills’. Originally I tried to set their spawn points to the arena but I couldn’t figure out how, so now i’m trying to just teleport them back to the arena when they die, but thats not working either. I also want to regive the sword to the player that died when they respawn. If anyone could help me I would be extremely grateful.

Here is the part of the script that isn’t working:

local function startEvent()
	
	local plr1CurrentKills = 0
	local plr2CurrentKills = 0
	
	local SwordGiver = game.ServerStorage.Sword
	
	local plr1 = standingOnArena1Player1
	local plr2 = standingOnArena1Player2
	
	local Sword1 = SwordGiver:Clone()
	local char1 = plr1.Character
	local Sword2 = SwordGiver:Clone()
	local char2 = plr2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
	
	
	print("Variables working fine")
	
	
	if char1 or char2 then
		
		
		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")
		

		if humanoid1 or humanoid2 then
			
			Sword1.Parent = plr1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = plr2.Backpack
			humanoid2:EquipTool(Sword2)
			
			print("Approved that i'm humanoid")

			while plr1CurrentKills and plr2CurrentKills < 5 do
				wait(1)
				char1.Humanoid.Died:Connect(function(tag)
					print("Detected Death")
					plr2CurrentKills = plr2CurrentKills + 1
					char2.Humanoid.Health = 100
					print("Set char2 health")
					wait(4)
					print("Waited")
					Sword1.Parent = plr1.Backpack
					humanoid1:EquipTool(Sword1)
					print("Regave sword")
					char1.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player1Spawn.Position
					)
					char2.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
					print("Teleported")
				end)
				char2.Humanoid.Died:Connect(function(tag)
					plr1CurrentKills = plr1CurrentKills + 1
					char1.Humanoid.Health = 100
					wait(2)
					Sword2.Parent = plr2.Backpack
					humanoid2:EquipTool(Sword2)
					char1.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player1Spawn.Position
					)
					char2.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
				end)
			end		
		end
	end		
end

And here is the whole script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2

local team1 = workspace.Arena1Team1
local team2 = workspace.Arena1Team2

local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil


local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")


local function startEvent()
	
	local plr1CurrentKills = 0
	local plr2CurrentKills = 0
	
	local SwordGiver = game.ServerStorage.Sword
	
	local plr1 = standingOnArena1Player1
	local plr2 = standingOnArena1Player2
	
	local Sword1 = SwordGiver:Clone()
	local char1 = plr1.Character
	local Sword2 = SwordGiver:Clone()
	local char2 = plr2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
	
	
	print("Variables working fine")
	
	
	if char1 or char2 then
		
		
		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")
		

		if humanoid1 or humanoid2 then
			
			Sword1.Parent = plr1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = plr2.Backpack
			humanoid2:EquipTool(Sword2)
			
			print("Approved that i'm humanoid")

			while plr1CurrentKills and plr2CurrentKills < 5 do
				wait(1)
				char1.Humanoid.Died:Connect(function(tag)
					print("Detected Death")
					plr2CurrentKills = plr2CurrentKills + 1
					char2.Humanoid.Health = 100
					print("Set char2 health")
					wait(4)
					print("Waited")
					Sword1.Parent = plr1.Backpack
					humanoid1:EquipTool(Sword1)
					print("Regave sword")
					char1.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player1Spawn.Position
					)
					char2.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
					print("Teleported")
				end)
				char2.Humanoid.Died:Connect(function(tag)
					plr1CurrentKills = plr1CurrentKills + 1
					char1.Humanoid.Health = 100
					wait(2)
					Sword2.Parent = plr2.Backpack
					humanoid2:EquipTool(Sword2)
					char1.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player1Spawn.Position
					)
					char2.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
				end)
			end		
		end
	end		
end

RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
		--Hide playbutton gui
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
		--Teleports
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
		local gameRunning = true
		startEvent() -- triggers function
	end
end)


-----------------------------------------------------

--				PART1 Events				 --

-----------------------------------------------------

-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]

part1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player1 = Player
		end	
	end
end)

part1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

-----------------------------------------------------

--				PART2 Events				 --

-----------------------------------------------------

part2.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied

			part2.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player2 = Player
		end	
	end
end)

part2.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
			if   standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
			end
			part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player2 = nil

			if standingOnArena1Player1 ~= nil then
				if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

while task.wait(0) do
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
	end
end

Thanks for any help!

1 Like

Did you try using character:MoveTo(position). and try humanoid.Died function to regive the sword

Maybe look at this for teleportation.