How to fix delay in remote events?

I am using a .touched event that activates a remote event to turn off CanTocuh on that part but I have discovered there is a delay between when the event gets fired so the part actually gets multiple touched requests and fires the server multiple times. Now I tried to make a debounce where once it ran all the code on the client side it would call a remote event back to the server side script to turn off the debouce but it does not work so what would be the most effective way to solve this?

I also tried a debounce of like 0.3 seconds it mostly worked but based on how good the clients wifi is and if they stay in the part after they got touched the touched event would fire twice or up to 8 times. It works if I put the debouce to 1 second but then if there were two gems right next to each other they would not be able to just walk through them they would have to wait. So if I ever made a level where you need to jump down a hole and while they’re falling they touch gems it would not work.

The next time don’t post another question but rather pump it with a reply, or wait over someone that can help. What are you trying to do from the very first start? I mean by this, what is the mechanic you’re trying to create over.

If I recall correctly, you’re doing some gem thing, right? If this is the case, render the gem in the client (meaning that all the players have the gem, but the server can’t really tell if there’s one at all) and connect the <BasePart>.Touched event in there, send the request to the server and from there handle your logic.

If you can explain with further detail what you’re trying to replicate or create, it would be really helpful. Also when asking for SCRIPTING help, you should PROVIDE code.

1 Like

So in a for loop it goes through all the gems and if one is touched it adds one gem to their leaderstats value and also calls a remote event to a local script that makes it transparent and turn can collide off only for them

I will ignore the fact that this will be easily exploitable anyway, as I said, you should be sharing code and I’d love to know what is your problem at all, what do you mean by “delay”?

1 Like

The problem is that multiple touched events are going through because when I call the fireserver event there is a delay between when it accutualy gets fired. What is supposed to happen is that when the event is fired on the local script it turns cantouch off but because of the delay two or even up tp 8 touched events can go through before the event turns cantouch off (I can’t provide code right now because my studio is having server connection errors)

Wait I found the code in another post:

Normal Script:

Gem.Touched:Connect(function(hit)
			print("Touched")
			local Player = GetPlayer(hit)
			if Player == false then return end
			AddGems:FireClient(Player, Gem, Level, Player) 
			Player.Gems.Gems.Value += 1
			Level.DisplayPart.SurfaceGui.TextLabel.Text = Player.Gems.Gems.Value.."/"..Player.Gems.TotalGems.Value

local script:

local Rep = game:GetService("ReplicatedStorage")
local AddGems = Rep:WaitForChild("AddGems")

AddGems.OnClientEvent:Connect(function(Gem, Level)
	Gem.CanTouch = false
	Gem.Transparency = 1
end)

There is more code for the normal script but since my studio wont connect I can’t get it sorry

Then use a debounce over to limit the times that a task can be executed, or every certain time. Example over:

local debounce = false
<BasePart>.Touched:Connect(
    function(hitInstance)
        if debounce then return end -- we limit it
        debounce = true
        ... -- your code

        task.wait(2) -- we wait over 2 seconds to let the touch happen again
        debounce = false
    end
)
1 Like

I would do a wait but then if there are two gems right next to each other they would have to wait to get them

Do task.spawn then and encapsulate your thing. Though a lot quicker solution would be:

local debounce = false
<BasePart>.Touched:Connect(
    coroutine.wrap(function(hitInstance)
        if debounce then return end -- we limit it
        debounce = true
        ... -- your code

        task.wait(2) -- we wait over 2 seconds to let the touch happen again
        debounce = false
    end)
)
2 Likes

Thanks for your feedback on pumping topics I will try to be better on the dev forum I think I will mess around with the task.wait() but I was just looking for a better solution. Thanks again! :slight_smile:

1 Like