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

Why is the player's death not being detected?

local player = game.Players.LocalPlayer
local char = player.Character
local storage = game:GetService("ReplicatedStorage")

char.Humanoid.Died:Connect(function()
	for i,v in pairs(storage.Contestants:GetChildren()) do
		if v.Name == player.Name then
			v.Dead.Value = true
		end
	end
end)

This script only works once, then never works again. It is a localscript in StarterGui and this happens in both studio and in-game. Help?

1 comment

0

The reason it only works once is because once the player respawns a new character is created. You're only defining that one character once so every time you die it's getting that same characters humanoid (which is nil because it was destroyed after death). Use a playeradded event with a characteradded event inside of it checking when the humanoid dies. Hope this helps :)

1 answer

Okay. There are a few things that I notice, in which should be changed. The biggest is that you are locally changing the BoolValue, which wouldn't be a good idea if your game was to go into FE. Assuming that this script is to always run, this is simply the way I would do it:

--A server script in workspace

local storage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			for index, child in pairs(storage.Contestants:GetChildren()) do
				if child.Name == player.Name then
					child.Dead.Value = true
				end
			end
		end)
	end)
end) -- Expect typos because I didn't test this in studio, just typed it out here

What I changed: - I made this a server sided script that I'd use for a FE game -Got rid of single lettered variables - Probably made a script that works

2 comments

0

How would this work...when the player is added, the humanoid wouldn't be dead?

0

^ Incorrect. This will bind the following to the character. Once the Humanoid dies, the .Died:Connect will work. I'm pretty sure this code works

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