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?
1 | function KillPlayer(part) |
2 | h = part.Parent:findFirstChild( "Humanoid" ) |
3 | if h ~ = nil then |
4 | h.Health = 0 |
5 | end |
6 | end |
7 |
8 | script.Parent.Touched:connect(KillPlayer) |
I can imagine this lagging if:
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.
You probably need to use a debounce.
If you don't know what a debounce is here's a basic explanation:
01 | local debounce = true |
02 |
03 | function touched() |
04 | if debounce = = true then |
05 | debounce = false |
06 | print ( "I have been touched!" ) |
07 | wait() -- How many seconds until it can be touched again |
08 | debounce = true |
09 | end |
10 | end |
11 |
12 | script.Parent.Touched:connect(touched) |
So if you wanted to apply this to your script then you would write something like this:
01 | local debounce = true |
02 |
03 | function KillPlayer(part) |
04 | if debounce = = true then |
05 | debounce = false |
06 | h = part.Parent:findFirstChild( "Humanoid" ) |
07 | if h ~ = nil then |
08 | h.Health = 0 |
09 | end |
10 | wait() |
11 | debounce = true |
12 | end |
13 | end |
14 |
15 | script.Parent.Touched:connect(KillPlayer) |
I hoped this helped you!