Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by
Arystov 45
7 years ago

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

Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

Answer this question