Game lags whenever I pickup tool

Whenever I pickup my tool everything lags, here is what I mean:
https://gyazo.com/ea95c5e6c377083d1a210311139cc9af

The 2 errors I’m getting:
Workspace.trainmaster_65.EventHandler:31: attempt to index nil with ‘Hitbox’
Workspace.trainmaster_65.EventHandler:16: attempt to index nil with ‘Triggered’

The Script:

local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")
local RunS = game:GetService("RunService")

local Players = game:GetService("Players")
local localPlayers = game:GetService("Players").LocalPlayer
local Char = script.Parent

--//DropPickupClient//--
RunS.RenderStepped:Connect(function()
	for i, Drop in pairs(game.Workspace.ItemLocations.Dropped:GetChildren()) do
		if Drop.ClassName ~= "LocalScript" then
			local TargetProximityPrompt = Drop:FindFirstChild("Handle"):FindFirstChildWhichIsA("ProximityPrompt")

			TargetProximityPrompt.Triggered:Connect(function(player)		
				TS:Create(TargetProximityPrompt.Parent.Hitbox.Highlight, TweenInfo.new(.25, Enum.EasingStyle.Circular), {FillTransparency = 1}):Play()

				script:WaitForChild("Search"):Play()
			end)

			TargetProximityPrompt.PromptShown:Connect(function(prompt)
				script:WaitForChild("PromptVisibleUI"):Play()

				TS:Create(TargetProximityPrompt.Parent.Hitbox.Highlight, TweenInfo.new(.25, Enum.EasingStyle.Circular), {FillTransparency = 0.5}):Play()
			end)

			TargetProximityPrompt.PromptHidden:Connect(function(prompt)
				--script:WaitForChild("PromptVisibleUI"):Play()

				TS:Create(TargetProximityPrompt.Parent.Hitbox.Highlight, TweenInfo.new(1, Enum.EasingStyle.Circular), {FillTransparency = 1}):Play()
			end)
		end
	end
end)

Now I do know the reason, it’s because of the renderstep, but without it the highlights won’t appear. What do I do??

You think you have Triggered, PromptShown, and PromptHidden connections created once, but you’re actually doing those connections every renderstepped which results in the lag you have. There are more connections than necessary.

2 Likes

Without renderstepped the highlights won’t show, is there any other alternative I can use?

Whats the purpose of using renderstepped? If its to iterate through workspace for objects, couldnt u just use collection service?

2 Likes

I’ve never heard of that, let me try it

1 Like

Sorry I think you might have already mentioned it in your post but I just wanted to pinpoint the source of the “lag” itself. I know my reply isn’t a solution but the errors you get indicate you have TargetProximityPrompt parented to nil and sometimes that TargetProximityPrompt isn’t found.

For one, if you’re using :FindFirstChild(), you need to use if-statements to check the variable exists. I am not sure what you’re doing but maybe occasionally there are tools dropped without Handle/ProximityPrompt?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.