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

Death wall that kills you is to laggy? Help.

So I got a game where there is an arena and people fight in it. Then I got an Event which triggers that 4 walls spawn and they shrink so all players are forced to move in the middle of the arena. Everything works fine etc BUT If I want to add a deathscript to the walls so they acutally kill you if you touch it, the performance will completly go down. I think its because it is checking everything it touches something to see if it is a hum or not.

Any Idea how to handle this performance wise and only kill the player?

function KillPlayer(part)
	h = part.Parent:findFirstChild("Humanoid")
	if h ~= nil then
		h.Health = 0
	end
end

script.Parent.Touched:connect(KillPlayer)

11 comments

0

u might wanna try h:TakeDamage(100)

0

or use breakjoints

0

or h:TakeDamage(h.MaxHealth)

2

Weird. It shouldn't cause a peformance drop

0

unless there is some other factor at play here

0

:TakeDamage() doesn't damage the player if they have a force field tho

0

just use debounce

0

Maybe try adding a wait() ? That always helps me with lag

0

Breakjoints didnt work, tried it too.

0

Breakjoints didnt work, i tried it too. so weird.

0

You guys need to read my text on top. Im moving the walls btw, so im using TweenService maybe that.

2 answers

✓ Accepted answer

I can imagine this lagging if:

  • You have this script copied in tens or hundreds of parts
  • The wall parts are very large

You say these parts are moving using TweenService. If the walls are also large, I'm not surprised it's lagging! You are likely asking Roblox to essentially scan a huge area and report to you every part that it can find every frame (since TweenService runs constantly).

To fix this, you need a different approach - every frame, use math to check whether a player is "in bounds" or else in contact with the death wall. If they're out of bounds (but still in the map area -- ex, not in a lobby), then set their health to 0.

2 comments

0

alright! Thats a nice answer. Thats probably it because the Walls are pretty huge due to the arena size. Thanks and I'LL use another code for it :)) Is it better to move it with CFrame then?

0

Switching to CFrame won't solve the problem (and Tweening is a good choice anyway) - you need to have a loop that checks to see if anyone is colliding with a wall every frame (ex a "while true do wait() --[[check players here]] end" loop).

You probably need to use a debounce.

If you don't know what a debounce is here's a basic explanation:

local debounce = true

function touched()
	if debounce == true then
		debounce = false
		print("I have been touched!")
		wait() -- How many seconds until it can be touched again
		debounce = true 
	end
end

script.Parent.Touched:connect(touched)

So if you wanted to apply this to your script then you would write something like this:

local debounce = true

function KillPlayer(part)
	if debounce == true then
		debounce = false
	    h = part.Parent:findFirstChild("Humanoid")
	    if h ~= nil then
	        h.Health = 0
	    end
		wait()
		debounce = true
	end
end

script.Parent.Touched:connect(KillPlayer)

I hoped this helped you!

5 comments

0

This might work. I'll try it later out, Thanks!

0

It didnt.. That is so weird. Im using TweenService to move the walls btw.

0

Maybe give me a example of moving the Object etc plus killing players?

0

Debounce won't help much - the original script doesn't have any yielding commands (so it's not needed for correctness); all it does is skip some of the Touch events.

0

Ah I see.

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