local debounce = false script.Parent.Touched:Connect(function() if debounce == true then return end debounce = true script.Parent.Transparency = 0.5 wait(1) script.Parent.Transparency = 1 script.Parent.CanCollide = false end)
is there any way to make this script more efficient or less delay, i tested it and the delay with the .Touched event is about 1 second.
Due to this script being executed on the server machine, you will need to remember that data takes time to travel -- thus nothing is immediate. From the given information I can assume that the reason for this delay is exactly this, latency. All parts that have their network ownership set to a client will have their physics as well as collisions calculated by that client. When you set a Touched event listener to an anchored part, the peer (a client or a server) starts listening for collisions on the object.
Now going back to the first statement of data taking time to travel: moving client parts and server parts will always be a little off-sync. This means that a server listening to collisions of a part will always get the signal a little later than the network owner sent it (unless the network owner is the server, but in your case I'm assuming the "toucher" is a player's character). There's simply no way around it, that delay will always exist. The faster the parts are, the more noticeable the effect is.
However, there is another issue in your code which has to do with the very same topic. A server making a change (such as changing a part's transparency) has to be replicated to the clients. It takes time for a client to receive this information thus causing a delay.
When you add these two together, you might reach a one second delay. The length of it depends on your connection to the server.
What you can do instead is have the clients listen to the collisions of the part by themselves. This removes the second replication step completely and works purely based off of what the client sees. Everything in that client's eyes will be smooth. In case you want to do something that isn't just visual effects, such as awarding the toucher 100 coins, you will of course want to have the server listening to the collisions as well.
There are great resources in the answer of this question if you want to study video game networking a little more.
First, I would verify if the event is your issue. In most cases .Touched is a very glitchy event. To ensure there is a delay. I would put a print() before the If statement. This is used to determine if the raw function is causing any issues. What do I mean by raw? That states the function without any code behind it, it is just a blank function. If there is a delay, then there isn’t really a known workaround. However, it is common the structure of your function may be causing the delay. The If statement on line four looks very weak. Try using another method of ensuring the debounce is not already active. Which brings me to my last point, your debounce. Denounces are not really the best method of ensuring the script cycles through completely before restarting. Although, it seems to be working for you. I have heard of people using raycasting instead of the Touched event. You may also consider looking into that. If you found this helpful, please consider upvoteing it. But all-in-all I don’t see much that might cause any issues with what you are doing other then those few key notes.
I don't think there's any delay with the Touched
event
You are using a debounce, which is in fact set to 1 second as shown by line 07: wait(1)
. Also, you should set debounce = false
at the end of your function so that it can run again - unless you mean for it to only work once, in which case there are better options than debounce.
The way a debounce works, is as long as the debounce variable is set to true
, the function can't run again. Generally, we set debounce = true
immediately after the script starts, then set it back to false right before the script ends. This means has to be fully executed from start to finish before it can be called again. In your function, you added a one second delay between function calls using wait(1)
which gives you a 1 second cooldown before you can touch the object again.
If you don't want the delay to be 1 second, change the wait
time or remove it altogether.