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