Lag spike while removing a part

I am removing parts as they are collected by a Player.

The character walk lags when I pick up the first part, but none of the parts afterwards will cause a lag spike.

It only happens if the character is walking while picking up the part.

Makes no sense I can figure.

Does anyone else see this happening?

Here is the code that removes the part:

local Part = script.Parent
local CanTouch = true

Part.Touched:Connect(function(OtherPart)
	if CanTouch == false then return end
	if not OtherPart then return end
	if not OtherPart.Parent:FindFirstChild("HumanoidRootPart") then return end
	CanTouch = false
	game.Debris:AddItem(script.Parent, 0.1)
end)

Here is the test file:

Glitch.rbxl (56.4 KB)

1 Like

Is this happening on client or in the engine itself?

1 Like

I think that the lag happens because the Touched event keeps firing repeatedly when the character moves over the part. To fix this, I provided you an updated version of the code, adding a small cooldown between each touch

local Part = script.Parent
local CanTouch = true

Part.Touched:Connect(function(OtherPart)
    if CanTouch == false then return end
    if not OtherPart then return end
    if not OtherPart.Parent:FindFirstChild("HumanoidRootPart") then return end

    CanTouch = false
    game.Debris:AddItem(script.Parent, 0.1)
    task.wait(0.2) 
    CanTouch = true
end)

let me know if it helps :slight_smile:

1 Like

Not sure how to answer that question.

It is a Server script, if that helps.

1 Like

No real need to add a debounce if the event is not to be fired again. Just disconnect it, or set BasePart.CanTouch to false and let the deletion of the script handle the rest.

There is also no need to wait 0.1 seconds to delete the part, @mc7oof

2 Likes

Thanks.

But, the part is being destroyed, so I don’t need CanTouch to turn back on.

Also, it only happens on the very first part pickup.

All remaining part pickups work without lag.

1 Like

or you can just use :Once()

Are you testing in studio?

We are experiencing the same issue when we delete a Model from the workspace.

When we test in game client, it doesn’t lag but when we test in studio it does.

2 Likes

I add a specific time because if I don’t the game will decide when to remove it.

I want it to be removed almost instantly.

1 Like

I published the file as a test and played it online.

The lag event did not occur in the published game online.

1 Like

The game will remove it on the next frame if no time is specified (the shortest time allowable)

1 Like

Then its probably a studio bug, see the post I’ve mentioned it.
Wait for few hours/days until it gets fixed.

2 Likes

Alright, thanks for the clarification, but I was also thinking that if the lag only happens with the first part pickup, it could be related to how the event is handled for the initial touch. You can try disconnecting the Touched event after the first trigger, so it won’t be called again, and ensure the part is removed immediately without any delay using game.Debris:AddItem(Part, 0)

local Part = script.Parent
local CanTouch = true

Part.Touched:Connect(function(OtherPart)
	if not CanTouch then return end
	if not OtherPart then return end
	if not OtherPart.Parent:FindFirstChild("HumanoidRootPart") then return end
	
	CanTouch = false 

	Part.Touched:Disconnect()
	game.Debris:AddItem(Part, 0)
end)
1 Like

That is not an available solution. You need to verify the collision was with a player first, otherwise a tumbleweed could take the coin and prevent further interaction

3 Likes

That is not how you disconnect a callback from an event

1 Like

The script is destroyed with the part, so I’m not sure a disconnect is needed.

Is it?

1 Like

I would first confirm whether or not the deletion of the script prevents multiple responses from player collision. If it doesn’t, then disconnecting the event or disabling CanTouch (the property, not as a debounce) will suffice

I tried disabling the CanTouch property and disconnecting the event, but the issue with the lag still persists when the first part is touched

What sort of lag is occurring exactly? This sort of feature should be implemented on the client VFX-wise. The player will collide with the part and notify the server. The server will verify the client is within a specific radius of the part before awarding them. The part does not need to exist on the server; the client will delete the part regardless of whether or not they’re rewarded. They’ll also delete the part if other clients collide with it first