Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how can I fix this script to correctly detect that the player has died?

this script is supposed to be a function to teleport 2 players after one of them has died. It appears that the script isnt detecting that the player is dead. Any help?

function EndGame()
		local players = game.Players:GetPlayers()
		local teleportPart = "RespawnPoint" 
		local char1 = game.Teams.Fighting1:GetPlayers()
		local char2 = game.Teams.Fighting2:GetPlayers()
		local hum1 = char1.character:WaitForChild("Humanoid")
		local hum2 = char2.character:WaitForChild("Humanoid")
		
		print ("the function has worked!")
		
		while hum1.Died
			   do print(char1.Name.." has died.")
				    print(char2.Name.." has won.")
					wait(4)
				    char1.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(267, 22.565, 216.5))
				    char2.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(267, 22.565, 216.5))
				end
		
		
		while hum2.Died
			    do print(char2.Name.." has died.")
				    print(char1.Name.." has won.")
					wait(4)
				    char1.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(267, 22.565, 216.5))
				    char2.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(267, 22.565, 216.5))
				end
		
	end

1 answer

Ok so first off, you do not need to use while loops to detect if someone has died, an if statement will do. Second, the function you put will only run once, so it's only gonna check if any players have died once. Instead of using a while loop, you can use the Events to check if they died. Try the following:

hum1.Died:Connect(function()
	--Your code here
end)

hum2.Died:Connect(function()
	--Your code here
end)

Try replacing the while loops with events, and if they do not work, just put them outside the function. If that still does not work, let me know.

This discussion is preserved as it was. New answers, comments, and votes are closed.