The Scripting Support sub-category is intended for all development support relating to programming on the Roblox platform. This includes questions ranging in difficulty from extremely basic to even the most technical of issues.
You may present your thread how you choose, but you are required to answer the following questions in your thread;
What are you attempting to achieve? (Keep it simple and clear)
What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)
What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)
You may then include any further details.
What are you attempting to achieve?
I have a system which is supposed to set a BoolValue under the player to false when a player’s Humanoid dies. The way I do this is by adding a BoolValue called “Surviving” to every player that joins under a players.PlayerAdded event, which works properly. Inside this event connected function is another function, which is connected to the Humanoid.Died as follows, “Humanoid.Died:Connect(function()”, where “Humanoid” is found using :WaitForChild() on the player’s character. When this event fires the function should run, setting the “Surviving” BoolValue’s value to false.
What is the issue?
The issue is that sometimes the Humanoid.Died event doesn’t fire. In some cases it works, though sometimes I’ve seen that even after dying a player is considered surviving.
What solutions have you tried so far?
I haven’t really, I’m quite clueless as to why this might be happening. I will edit the topic as I find new clues.
That’s about it, thank you for reading. Sorry if I have not formatted my text properly and all as this is my first DevForum topic.
Edit:
Here’s the basic structure of my script.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local surviving = Instance.new("BoolValue")
surviving.Parent = player
surviving.Value = true
surviving.Name = "Surviving"
local character
local humanoid
--Other things are here, as I am using this in the same script as my DataStores.
player.CharacterAdded:Connect(function()
character = player.Character
humanoid = character:WaitForChild("Humanoid")
--Other things again, this time related to an overhead BillboardGui.
end)
repeat wait() until humanoid ~= nil
print("Humanoid found!") --This always prints, I have verified.
humanoid.Died:Connect(function()
surviving.Value = false --The statement which should set the value to false.
end)
end)
From what you first posted (I’m not sure because I havent seen your code), you didn’t make any mention of the CharacterAdded event.
That code will work the first time the Character loads in (as Humanoid changes whenever a new Character is spawned in), however afterwards, you will have a new Character and therefore a new Humanoid.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local surviving = Instance.new("BoolValue")
surviving.Parent = player
surviving.Value = true
surviving.Name = "Surviving"
local character
local humanoid
--Other things are here, as I am using this in the same script as my DataStores.
player.CharacterAdded:Connect(function()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
--Other things again, this time related to an overhead BillboardGui.
humanoid.Died:Connect(function()
print(humanoid.Parent.Name .. " died")
surviving.Value = false --The statement which should set the value to false.
end)
end)
end)
EDIT: You should see a message in the output whenever you die.
Other than that, your code hooking on humanoid.Died is outside the Player.CharacterAdded listener, so it will only run for one humanoid until the player respawns.
Firstly, I would use the Variable that is given to you in the CharacterAdded Function
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local surviving = Instance.new("BoolValue")
surviving.Parent = player
surviving.Name = "Surviving"
surviving.Value = true
--Other things are here, as I am using this in the same script as my DataStores.
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
print("Humanoid found!")
--Other things again, this time related to an overhead BillboardGui.
if humanoid then
humanoid.Died:Connect(function()
print(tostring(player) .. 'has died. ')
surviving.Value = false --The statement which should set the value to false.
end)
end
end)
end)
Try this. It is a bit redundant checking if Humanoid is not equal to nil.
Thanks! I removed the redundant line and put the humanoid.Died event inside the player.CharacterAdded event and now it seems to be working with a 0% failure rate.