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

.Touched Event causing extreme lag?

I have been working on my game and each time I test it in studio it is absolutely fine, but as soon as I play in the normal game and hit something using the .Touched Event it makes the entire game lag enormously and the ms keeps rising by 200-1000 every second. I think this is caused by the .Touched event from what I've seen.

Is there possibly another way to make the .Touched event not lag / slow down? If so, how?

Thank you in advance.

2 answers

✓ Accepted answer

use debounce:

Wait's 5 seconds before you can use it again

local debounce = false

function Touch(Event)
 if debounce == false then
debounce = true
     -- script u want here
wait(5)
debounce = false
   end
end


script.Parent.Touched:Connect(Touch)

2 comments

0

Doesn't really explain anything at all :/

0

Why did you answer an already explained question? Also it clearly reads "Wait's 5 seconds before you can use it again" and he understood. - he clearly isn't that much of a newbie in the scripting community do not insult his intelligence indirectly.

#Debounce

Debounce is a commonly used feature which makes the script wait a certain amount of timew which can then fire your code;

If we use the most commonly used; "Touched Event Line", and add debounce you will realise it does not lag.

local PartName = "Part"

local db = false

workspace[PartName].Touched:Connect(function()
       if not db then

       db = true

       print("I was touched")
       wait(5)

        db = false
       end
end)

This will wait five seconds before firing the same action;

#Long Run-Lag

If you go in the dev-console; F9, then go to memory and press UntrackedMemory. UntrackedMemory, can cause a lot of lag, and many developers suffer from it

What causes UntrackedMemory?

This is a scale from worst to least;

1 - Connections 2 - While Loops 3 - Other repetitive stuff 4 - 5 - 6 -

Bad scripts example;

Connections;

script.Parent.Touched:Connect(function()
       print("Hi")
end)

Loops;

while true do
  wait()
  print("HI")

Every-time a connection occurs, it goes directly into the UntrackedMemory folder.

How to fix and good scripts

Connections;

script.Parent.Touched:Connect(function()
    print("Hi")
script.Parent.Touched:Disconnect()

Loops;

while true do
  wait()
  print("Hello")
break

2 comments

0

That was very helpful, thank you a lot. Will use this knowledge in the future!

0

No problem!!!

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