How do I assign a player to a variable (or something like that)

Hello, I have got a script that when ‘player1’ steps on a part their player gets assigned a variable called ‘standingOnArena1Player1’ and the same for ‘player2’ = ‘standingOnArena1Player2’.
However after they are teleported into the arena and given swords to fight, when one of them dies, they are supposed to teleport back to their start position (they fight first to 5 kills). But the problem i’m getting is only the player that stays alive is getting teleported back, the player that dies doesn’t get teleported anywhere. I have a feeling this is because when the player dies, they loose the value of ‘standingOnArena1Player1/2’ so when I try teleport standingOnArena1Player1/2 it doesnt teleport that player that dies because they are no longer standingOnArena1Player1/2. I was wondering if I could figure out who the player is that has been teleported into the arena and store that value until I decide they are no longer the player, that way if they die I can still teleport them.
Any help would be greatly appreciated!

Here is my 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(2)
					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
		part1.Transparency = 1
		part1.CanCollide = false
		part2.Transparency = 1
		part2.CanCollide = false
		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

1 Like

This may be happening because, upon death, all joints of a player’s character break. So, even though you’re moving the HumanoidRootPart, the other parts are left behind.

1 Like

So you suggest I do trial an error until I do the correct wait() time so that the player teleports?

Nah, you can use the Player:LoadCharacter() method to respawn a player, and then you can teleport them to the spawn and give them a sword.

Though your script’s connections to their old character would be broken, so if player 2 died, you would have to reassign char2 to their character after they respawn.

1 Like

I just added a wait (10) before they teleport and they don’t teleport so there is definatly an error in the script rather than them being teleported when they are dead…

When the character respawns, the old model is destroyed and the script connections to it are broken. You have to reassign the char to their character after they respawn.

1 Like

How would I do this, sorry i’m pretty bad at scripting…thanks

You can accomplish it by incorporating something like this into what you already have:

char.Humanoid.Died:Connect(function()
  local playuh = game.Players:GetPlayerFromCharacter(char)
  playuh:LoadCharacter()
  char = playuh.Character
end)

Lemme know if something doesn’t work.

1 Like

Thanks it works great, the players are teleporting although for some reason the inventory of the person that dies is duplicating a bunch of times, but im sure i can fix that. How do I do the player:CharacterLoad method you mentioned here?

It’s a method you can call on a player object. Something like game.Players.Player12345:LoadCharacter() should work, but let me know if it doesn’t.

1 Like

Thanks, what is this ‘player12345’ is that where i place the players name?

You could, but using a variable of the player object works too, which is what I did in the snippet I sent earlier.

game.Players:GetPlayerFromCharacter(character model) -- returns the player that owns the character
1 Like

Hi, thank you very much for your help, for some reason my script isn’t working properly, when the player dies, they respawn and their whole inventory dupes multiple times and also they are not getting teleported.
Here is the script (only the part i’m editing):

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
	
	local character1Loaded = false
	local character2Loaded = false
	
	
	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)
					
					local player1 = game.Players:GetPlayerFromCharacter(char1)
					player1:LoadCharacter()
					char1 = player1.Character

					plr2CurrentKills = plr2CurrentKills + 1
					
					char2.Humanoid.Health = 100

					game.Players.player1:LoadCharacter()
					character1Loaded = true
					
					if character1Loaded then
						Sword1.Parent = plr1.Backpack
						humanoid1:EquipTool(Sword1)

						char2.HumanoidRootPart.CFrame = CFrame.new(
							Arena1Player2Spawn.Position
						)
						char1.HumanoidRootPart.CFrame = CFrame.new(
							Arena1Player1Spawn.Position
						)
						character1Loaded = false
					end
				end)
				char2.Humanoid.Died:Connect(function(tag)
					
					local player2 = game.Players:GetPlayerFromCharacter(char2)
					player2:LoadCharacter()
					char2 = player2.Character
					
					plr1CurrentKills = plr1CurrentKills + 1
					
					char1.Humanoid.Health = 100
					
					game.Players.player2:LoadCharacter()
					character2Loaded = true
					
					if character2Loaded then

						Sword2.Parent = plr2.Backpack
						humanoid2:EquipTool(Sword2)

						char1.HumanoidRootPart.CFrame = CFrame.new(
							Arena1Player1Spawn.Position
						)
						char2.HumanoidRootPart.CFrame = CFrame.new(
							Arena1Player2Spawn.Position
						)
						character2Loaded = false
					end
				end)
				wait(1)
			end		
		end
	end		
end

Thanks again for all your help!

btw I added the ‘character1Loaded’ to try get rid of the duplication problem, but it did nothing.

Just under your print statement, at the check for plr1CurrentKills, it should be plr1CurrentKills < 5 instead. Change that and let me know what changes.

1 Like

Ok, sure thanks but I need it to detect that both player1 and player2 kills are under 5, would that still work?

Just changed it and nothing seemed to change, the dead players whole inventory duplicated and neither players teleported

If you want to check if they’re under 5, then the operator you would use without flipping anything around would be <=.

Before you made the change to that check, did one of the players teleport, or was it the same?

1 Like

Before i added the ‘game.Players.player1:LoadCharacter()’ one of the characters teleported, but since then neither characters have teleported.

I didn’t explain, so that’s my fault, but you should only use the LoadCharacter method on the player that died.

1 Like