If you assign a player to a variable and they die, does it not work anymore?

I have this script for after two players have fought. The “winner” is the player that didnt die, and the loser is the player that died. For some reason when this script is ran, it works perfectly for the “winner” but the losers walkspeed is still 16 and they don’t have a forcefield as they can die. I get no error messages, any idea why?

local function disconnectConnections(kills, loserPlayer)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
						loser = loserPlayer
					elseif plr2CurrentKills == 5 then
						winner = player2
						loser = loserPlayer
					end			

					player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					
					winner.leaderstats.Wins.Value += 1
					winner.leaderstats.Winstreak.Value += 1
					loser.leaderstats.Winstreak.Value = 0
					
					local ff1 = ff:Clone()
					local ff2 = ff:Clone()
					
					ff1.Parent = winner.Character
					ff2.Parent = loser.Character
					ff1.Visible = false
					ff2.Visible = false
					winner.Character.Humanoid.WalkSpeed = 25
					loser.Character.Humanoid.WalkSpeed = 25

					
					local username = loser.Name
					local winnerName = winner.Name
					local winnerId = winner.UserId

Check where you are changing your variables, that is most likely the reason.

After a players death the player character no longer exists and the old one simply does not respawn but rather is removed. I’d suggest using local character = player.CharacterAdded:Wait()
Which would change the character variable as soon as the player respawns.

1 Like
local Character = Player.Character or Player.CharacterAdded:Wait()

pretty sure thats good :+1:

1 Like

Yes, but remove Player.Character as it is basically a one time use only.

Because the Player.Character after destroying would stay nil and not using that rate using CharacterAdded would change it with the new character thus mot breaking the entire code. :expressionless:

1 Like

This is the updated code and it’s still not working. Theres no error messages in the output.

local function disconnectConnections(kills, loserPlayer)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
						loser = loserPlayer
					elseif plr2CurrentKills == 5 then
						winner = player2
						loser = loserPlayer
					end	
				

					player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					
					winner.leaderstats.Wins.Value += 1
					winner.leaderstats.Winstreak.Value += 1
					loser.leaderstats.Winstreak.Value = 0
					
					local ff1 = ff:Clone()
					local ff2 = ff:Clone()
					
					local loserChar = loser.CharacterAdded:Wait()
					
					ff1.Parent = winner.Character
					ff2.Parent = loserChar
					ff1.Visible = false
					ff2.Visible = false
					winner.Character.Humanoid.WalkSpeed = 25
					loserChar.Humanoid.WalkSpeed = 25

					
					local username = loser.Name
					local winnerName = winner.Name
					local winnerId = winner.UserId

a lot if people say player.Character
or player.CharacterAdded:Wait() is recommended

because if the player died it will just move into player.CharacterAdded:Wait(), ignoring player.Character

yes i guess maybe its the same thing, but some people say its better

Yea, they are wrong. Youcan check this yourself. Insert a script in starterplayerscripts and test it out.

I would help you but die to being om mobile I am unable to. Sorry!

Though, maybe try doing:

workspace:FindFirstChild(loser.Name)
Or just do:
loser.Character

1 Like
repeat wait() until player.Character ~= nil
1 Like

i tested it using print statements and everything works fine?

Any ideas how to fix it as it’s still not working?

local function disconnectConnections(kills, loserPlayer)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
						loser = loserPlayer
					elseif plr2CurrentKills == 5 then
						winner = player2
						loser = loserPlayer
					end	
				

					player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					
					winner.leaderstats.Wins.Value += 1
					winner.leaderstats.Winstreak.Value += 1
					loser.leaderstats.Winstreak.Value = 0
					
					local ff1 = ff:Clone()
					local ff2 = ff:Clone()
					
					local loserChar = loser.CharacterAdded:Wait()
					
					ff1.Parent = winner.Character
					ff2.Parent = loserChar
					ff1.Visible = false
					ff2.Visible = false
					winner.Character.Humanoid.WalkSpeed = 25
					loserChar.Humanoid.WalkSpeed = 25