How can I optimise this code? (Pickpocket System)

Hi, I have made a pickpocket system. Basically, if the player stands still for 20 seconds, a proximity prompt will be enabled, but when the player is moving, the proximity prompt is disabled

It works but I am just wondering if there is any way to improve it, if there are 50+ players standing still in game then I can imagine the game might start lagging right?

Here is the code, let me know if you have any suggestions:

Players.PlayerAdded:Connect(function(plr)

	plr.CharacterAdded:Connect(function(char)
		local HRP = char:WaitForChild('HumanoidRootPart')
		
		PickpocketPrompt:Clone().Parent = HRP
				
		local DelayFunc
		Humanoid.Running:Connect(function(speed)
			if speed == 0 then
				if DelayFunc == nil then
					DelayFunc = task.delay(20), function()
						print('Delay finished')
						if speed == 0 then --If plr speed is still 0 after 20 secs
							HRP.PickpocketPrompt.Enabled = true
						end
					end)
					
				end
				
			else
				HRP.PickpocketPrompt.Enabled = false
				if DelayFunc then
					task.cancel(DelayFunc)
					DelayFunc = nil
				end
			end
		end)

	end)

Thank you

2 Likes