Delay in onTouch script

In my game, you pick up eggs to buy upgrades. The problem is when a player runs into an egg, it takes a second for the egg to be picked up. The onTouch event for the egg doesn’t have any waits in it. It just checks if the thing that touched it is a player, and removes the egg. How would I get rid of that 1 second delay before the egg gets picked up?

function onTouch(hit)
	local h = hit.Parent:findFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if h ~= nil then
		script.Parent:remove()
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GamepassId) then
		player.leaderstats.Eggs.Value = player.leaderstats.Eggs.Value + (((player.leaderstats["Egg Multiplier"].Value) + 1) * player.leaderstats.Rebirth.Value) * 2
		InsertEgg:Fire()
	else
		player.leaderstats.Eggs.Value = player.leaderstats.Eggs.Value + ((player.leaderstats["Egg Multiplier"].Value) + 1) * player.leaderstats.Rebirth.Value
		InsertEgg:Fire()
	end
end
end
script.Parent.Touched:Connect(onTouch)
1 Like

Could you please provide the code so we could help you? It is extremely hard to provide help to someone when there is no code provided to fix the issue.

There could probably be other things in your code that yields, or it is probably your poor internet connection. Please provide the code you used

The issue is probably MarketplaceService:UserOwnsGamePassAsync as it yields the script until it’s done checking if the player owns the gamepass

How would I make it check faster?

First off, remove is deprecated in favour of destroy, reason being is because the objects will still remain in memory. UserOwnsGamePassAsync is a yielding function, and you can’t make it go faster

1 Like

It removes the egg before it checks if the player owns the gamepass though

The only thing I think of right now is to wrap it in a separate function in a coroutine or something like that (not sure if it’ll work but you could try)

Another option that should work is to cache the results (of userowns async), unless for some reason you need this to be checked everytime the player touches the part

1 Like

Instead of checking if the player owns the gamepass every single time they collect an egg, which would cause the delay every time, you could instead check as soon as they join and add a “VIP” value under Player and then just scan the Player for that value instead.