Killbrick lags the game a lot?

In short, I want to create a normal kill brick. However I am struggling on the code.
This is my code:

x = getfenv
x().Brick = Instance.new("Script", script).Parent.Parent
x().script:ClearAllChildren()

x().getfenv().Instance.new("Part").Touched.connect(x().Brick.Touched, function(hit)
	humanoid = Instance.new("Part", hit.Parent).FindFirstChildOfClass(hit.Parent, "Humanoid")
	if humanoid == nil then else
		Instance.new("Humanoid", workspace).TakeDamage(humanoid, 100)
	end
	return false
end)

It does kill the player, but it lags. A lot. That’s the issue.
pls help

4 Likes

There are a couple of key issues:
- Parenting a humanoid to workspace. (This is the main issue)
- Lack of a debounce

Adding a humanoid to workspace is known to create a lot of lag. Instead, leave the created humanoid parented to nil (just do Instance.new("Humanoid")).

Additionally, it is firing a LOT. Try adding a debounce to check if the humanoid is already being processed.

(or just don’t do the coding like how you’ve done it..)

4 Likes

What’s necessarily wrong about it? I think it’s pretty good myself.

3 Likes

Purely for the sake of any (likely newer) scripters who saw it and are genuinely curious, these are some of the main issues:

  • You should not be using getfenv. At all. Just use normal variables.
  • Rather than using Instance.new in order to call functions like TakeDamage and FindFirstChildOfClass, you should instead use : rather than ., in order to pass the instance automatically (Instance.new("Humanoid").TakeDamage(humanoid, 100) → humanoid:TakeDamage(100))
  • the humanoid variable should be local, rather than global (humanoid = ... → local humanoid = ...)
  • There’s no reason to have return false at the end of the function.

(I am aware that the post is a troll… at least, I hope it is…)

1 Like

why on earth are you using getfenv???

3 Likes

Maximum efficiency or smth idk (it’s a non-serious post xD)

1 Like

why do you use getfenv for that :sob:

This is how I would do it:

local DamageParts = script.Parent

for i, Part in pairs(DamageParts:GetChildren()) do
	
	if Part:IsA("Part") then
		
		Part.Touched:Connect(function(Hit)
			
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			
			if Humanoid then
				Humanoid.Health = Humanoid.Health - 25
			end
			
		end)
	end
	
end

The parts and the script would be in a folder.

1 Like

Niklantis has landed. This is truth. Except for pairs

1 Like

Uhhh, wouldn’t this error? Since you have to get script from getfenv…?

What should I use instead?

no :sob:

You should use nothing, like so:

for _,__ in t do

Huh?

1 Like

bro stop the ragebait

You don’t need getfenv for script.Parent
No error at all

I really like it when ragebaiters call other people ragebaiters. Everyone here, even non scripters, know that you have to use getfenv() to get the script. Lol

4 Likes

I’m a scripter and I don’t agree with ysing getfenv(). For a killbrick, I’d agree with @Niklantis05. Both with the code, and the ragebait post. I’ve been scripting for 2+ yrs and i’ve never had to use “getfenv()”. Even AI can spot that this is incorect. Also it thinks the code you pasted in your example code, is unsafe. in reality, use something like what @Niklantis05 did.

local damagePart = script.Parent
local damageAmount = 100

damagePart.Touched:Connect(function(hit)
    local character = hit.Parent
    if character then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            humanoid:TakeDamage(damageAmount)
        end
    end
end)

1 Like

Highly disagree btw. Non scripters wouldn’t know what getfenv() is even if their life depended on it. To me this is ragebait or something. Anyways, scripters prob wouldn’t use getfenv() either. I mean a lot of devs use killbricks and 80% of them or smth like that normally don’t have to post a devforum post for it.

2 Likes

ive been scripting for almost 2 years and i didint use getfenv a SINGLE time in my life :broken_heart:
If that’s your ‘everyone knows this’, idk what era you coding in :sob:

2 Likes

road blocks programming learning 101

its a troll post

charsbarslarskars

Isn’t that even deprecated?

No, getfenv is the only way to get the script. the “script” global is deprecated.

5 Likes