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

Hello What is best way to track when humanoid is killed and put on a value???/

I put script in ServerScriptStorage

value = script.Killed

-- my attempt

while wait(1) do
	for _,v in ipairs(workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and not v:FindFirstChild("tracked") then
			spawn(function()
				repeat wait() until v.Humanoid.Health <= 0
				value.Value = value.Value + 1
				t = Instance.new("StringValue")
				t.Parent = v
				t.Name = 'tracked'
			end
		end
	end
end

that is my best attempt from scratch(no testing involved) i would like to know if there are better ways to do this as will use many resources in the game you know? thanks you for the time.

000 EDIT - Additionally this is how i would track who the killer of humanoid was : 000

while wait(1) do
	for _,v in ipairs(workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and not v:FindFirstChild("tracked") then
			spawn(function()
				repeat wait() until v.Humanoid.Health <= 0
				value.Value = value.Value + 1
				t = Instance.new("StringValue")
				t.Parent = v
				t.Name = 'tracked'

				if v.Humanoid:findFirstChild("creator") and v.Humanoid.creator.Value ~= nil and not script:FindFirstChild(v.Humanoid.creator.Value.Name) then
					player = Instance.new("IntValue", script)
					player.Value = player.Value = 1
					player.Name = v.Humanoid.creator.Value.Name
				elseif v.Humanoid:FindFirstChild("creator") and v.Humanoid.creator.Value ~= nil and script:FindFirstChild(v.Humanoid.creator.Value.Name) then
					player = script:WaitForChild("v.Humanoid.creator.Value.Name)
					player.Value = player.Value + 1
				end
			end
		end
	end
end

2 answers

humanoid.Died would make things a lot easier for you.

As Arystov said, Use Humanoid.Died which is explained on the Wiki Here. Essentially, just edit the example on the website to get your desired result, in which the use of a function is used to replace a 'While Loop'.

game:GetService('Players').PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			--Edit your code inside your While loop to place here
		end)
	end)
end)

Or

local value = Script.Killed

game:GetService('Players').PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			value.Value = value.Value + 1
                t = Instance.new("StringValue")
                t.Parent = v
                t.Name = 'tracked'

                if v.Humanoid:findFirstChild("creator") and v.Humanoid.creator.Value ~= nil and not script:FindFirstChild(v.Humanoid.creator.Value.Name) then
                    player = Instance.new("IntValue", script)
                    player.Value = player.Value = 1
                    player.Name = v.Humanoid.creator.Value.Name
                elseif v.Humanoid:FindFirstChild("creator") and v.Humanoid.creator.Value ~= nil and script:FindFirstChild(v.Humanoid.creator.Value.Name) then
                    player = script:WaitForChild("v.Humanoid.creator.Value.Name)
                    player.Value = player.Value + 1
                end
            end
        end
		end)
	end)
end)

P.S. Someone may want to check the code in the second block

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