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

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

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

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)

2 comments

0

you don't have to define it inside...

0

If the game is dynamic and the number of Noobs changes, yes you do ;D

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)
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)


1 comment

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

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))

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