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

How do I use a For Loop to kill players with the name "Noob" ?

Asked by 7 years ago

I want to kill the players with the name "Noob" when someone touches a brick but I don't know how to do that in my code

local noobs = workspace:GetChildren()

function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if h~=nil then
        for i,v in pairs(noobs) do
            if v.Name == "Noob" then

            end
        end
        wait(0)
    end
end

script.Parent.Touched:connect(onTouched)

4 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You can use the BreakJoints function to kill character models :)

Note: to get live results whenever the part is touched, you have to define 'noobs' inside the onTouched function

function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    local noobs = workspace:GetChildren() --Define inside
    if h then
        for i,v in pairs(noobs) do
            if v.Name == "Noob" and v.Humanoid.Health ~= 0 then
                v:BreakJoints() --BreakJoints function
            end
        end
        wait()
    end
end

script.Parent.Touched:connect(onTouched)
0
you don't have to define it inside... thehybrid576 294 — 7y
0
If the game is dynamic and the number of Noobs changes, yes you do ;D Goulstem 8144 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
local noobs = workspace:GetChildren()

function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if h and h~=nil then
        for i,v in pairs(noobs) do
            if v.Name == "Noob" then
        if v:FindFirstChild("Humanoid") then
            v.Health = 0
        end
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)
Log in to vote
0
Answered by 7 years ago
local noobs = workspace:GetChildren()

function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if h~=nil then
        for i,v in pairs(noobs) do
            if v.Name == "Noob" then

            end
        end
        wait(0)
    end
end

script.Parent.Touched:connect(onTouched)


Not sure why you want to do this, but you can use Humanoid:TakeDamage(x) to directly remove x HP from the player. Like this:

local noobs = workspace:GetChildren()

function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if h~=nil then
        for i,v in pairs(noobs) do
            if v.Name == "Noob" then
                 v.Humanoid:TakeDamage(1000000000000000)
            end
        end
        wait(0)
    end
end

script.Parent.Touched:connect(onTouched)


0
v.Humanoid:TakeDamage(v.Humanoid.MaxHealth) will work as well if you are making a super hardcore game, and remove their maxhp, which is all of their HP that they could have, therefore always killing them DaCrazyDev 444 — 7y
Log in to vote
0
Answered by
Master_JJ 229 Moderation Voter
7 years ago

I found my way of going about this more efficient. For this to work put the script into the part.

local part = script.Parent

part.Touched:connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then -- Checks if a player hit it
        for i,v in ipairs(game.Players:GetPlayers()) do -- Loops through all players
            if v.Name == "Noob" then -- If their name is "Noob"
                v.Character:BreakJoints() -- Kill them by breaking joints
            end
        end
    end
end))

Answer this question