How can I speed up my touched event?

So I have a food eating system for my game, where it checks certain requirements before actually destroying itself and adding the stats for the player. The problem is, this waiting time can take up to 1-2 seconds, which leads to a noticeable delay when players try to eat the food. For example, a player could touch the food and be 10 studs away and suddenly see the food get destroyed. Is there any thing that is specifically doing this and is there a way I can speed this script up?

rpvefood.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				if hit.Parent.Humanoid.Health > 0 and plr then 
					local data = GetData(plr)
					if data then
						if data.GameData.PVP.Value == false then
							if data.GameData.Size.Value < 500 then
								rpvefood:Destroy()
								data.Totals.TotalFoodEaten.Value = data.Totals.TotalFoodEaten.Value + 1
								data.Totals.FoodEaten.Value = data.Totals.FoodEaten.Value + 1
								if data.Gamepasses.DoubleMass.Value == true then
									data.GameData.Size.Value = data.GameData.Size.Value + 2
									if data.GameData.Size.Value > 500 then
										data.GameData.Size.Value = 500
									end
								else
									data.GameData.Size.Value = data.GameData.Size.Value + 1
								end
								if data.Gamepasses.DoubleCoins.Value == true then
									data.Totals.Coins.Value = data.Totals.Coins.Value + 2
									game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 2, "Coins", Color3.fromRGB(255, 255, 0))
								else
									data.Totals.Coins.Value = data.Totals.Coins.Value + 1
									game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 1, "Coins", Color3.fromRGB(255, 255, 0))
								end
								IncreaseXP(plr, 1)
								SetSize(plr)
								SpawnFood(1)
							else
								game.ReplicatedStorage.Remotes.ErrorMessage:FireClient(plr, "Max size for PVE reached! Enable PVP to grow bigger!")
							end
						elseif data.GameData.PVP.Value == true then
							if data.GameData.Size.Value < 4000 then
								rpvefood:Destroy()
								data.Totals.TotalFoodEaten.Value = data.Totals.TotalFoodEaten.Value + 1
								data.Totals.FoodEaten.Value = data.Totals.FoodEaten.Value + 1
								if data.Gamepasses.DoubleMass.Value == true then
									data.GameData.Size.Value = data.GameData.Size.Value + 2
									if data.GameData.Size.Value >= 4000 then
										data.GameData.Size.Value = 4000
										game.ReplicatedStorage.Remotes.ErrorMessage:FireClient(plr, "Max mass reached!")
									end
								else
									data.GameData.Size.Value = data.GameData.Size.Value + 1
								end
								if data.Gamepasses.DoubleCoins.Value == true then
									data.Totals.Coins.Value = data.Totals.Coins.Value + 2
									game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 2, "Coins", Color3.fromRGB(255, 255, 0))
								else
									data.Totals.Coins.Value = data.Totals.Coins.Value + 1
									game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 1, "Coins", Color3.fromRGB(255, 255, 0))
								end
								IncreaseXP(plr, 1)
								SetSize(plr)
								SpawnFood(1)
							end
						end
					end
				end
			end
		end)
1 Like

Hm, well I’d say it’s worth noting if you have a lot of processes running or are consuming an increasing amount of memory, that’d make sense as to why such a big delay is present. I’m looking at your code, and it’s kind of surprising that it would cause a delay that much under normal circumstances, unless I missed something, I sort of just skimmed it.

If the performance of your game seems good, I might suggest creating events for the food client-sided, then utilizing Remote Event calls to the server for verification and such to avoid exploitation, that’d greatly speed things up I would assume.

Hope this helps :slight_smile:

Does your GetData function have a Wait or anything that might yield the current thread? I’m wondering this because your rpvefood:Destroy() , which is what I assume does “food destroying” that you mention, comes after that function call.

1 Like

GetData doesn’t yield at all, and doesn’t have any sort of wait. It’s just a shorter version of game.ReplicatedStorage.PlayerData:FindFirstChild(plr.Name)

That’s interesting that you’d write out a whole other function to replace, as you just wrote it out, one line of code that (at least in my opinion) is pretty readable/straightforward. If that’s not the issue and you haven’t already, then do some debugging to make sure the .Touched fires when you want it to (maybe something wrong with the food’s hitbox?), and if in between the event firing and the :Destroy() lies this delay. All in helping pin point what is causing this delay.

I just found out that the problem is because the touched event doesn’t fire. If a player touches it and walks away, it sometimes fires then, which is weird.

Does memory usage seem high in your game at all??

You may want to change plr to plr ~= nil. I am not sure if the script is even continuing past that point.

You should insert some prints (one at the beginning of the function before any if statements, and one after each consecutive if statement (or just important ones that you arent sure if the script is making it past).

This way, you can tell if the touched event fires (with no delay since the print is at the start of the function), and if the code actually keeps going throughout (it may not execute the intended code if an if statement fails).

Same thing, Lua will check if plr is nil or false. If it’s not either, that part will evaluate to true.

You are right, I don’t know why that slipped my mind. It seemed dubious to me, but now I recall using it that way numerous times… :sweat_smile:

Regardless, if the OP inserts some prints we can get a better idea of exactly where things are going wrong. It seems unlikely to me that the fault lies on the Touched event firing.