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

Why doesn't the Humanoid.Died function work properly?

I am making a script to change the player's team when they die, and using the .Died function on the Humanoid to do so. I do not get any errors, but when the computer gets to this part in the script, the player's team changes to tu even though they didn't die.(All of these teams are defined in an earlier part of the script) I don't know why this is happening but here's my code, which is in a server script, but I have filtering enabled off so I should be able to use the LocalPlayer command. (I know I should have filtering enabled on but I am just trying something with it off.)

	while true do 
		local plr = game.Players.LocalPlayer.Character.Humanoid
		local player = game.Players.LocalPlayer	
		if plr.Died then
			if player.Team == t1 or player.Team == t2 or player.Team == t3 or player.Team == t4 then
				player.Team = tu					
			end
		end

		wait(2)
	end

Thank you! Let me know if you need more information.

2 answers

An event is meant to take the place of an infinite loop. Just get rid of the loop and say:

plr.Died:Connect(function()

	-- code here

end)

Also it needs to be a LocalScript for you to get the LocalPlayer. No matter what.

1 comment

0

^

###What you tried to do is check if the Died event exists, or if it equals true. Since all values that are not false or nil in Lua are considered truthy, it's the same as saying:

if true then

###Won't get what you want. You have to connect the event property with RBXScriptSignal:Connect(). After that, your script will return an RBXScriptConnection.

plr.Died:Connect(function()
    -- Do code, and don't use a while loop for nothing.
end)

###Final product:

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() 
local hum = char.Humanoid

hum.Died:Connect(function()
    if player.Team == t1 or player.Team == t2 or player.Team == t3 or player.Team == t4 then
        player.Team = tu                    
    end
end)

###For next time, make sure your variables correspond to their values, as it helps others understand better. And don't say it wasn't functioning properly, when you didn't use it correctly. Just a few tips.

4 comments

0

You outshone my answer lol.

0

I was typing all of that while your answer was already there lol

0

Thank you a lot for the fix and the advice.

0

Welcome. We are scriptinghelpers.org for a reason.

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