Lag when changing CanCollide

In a local script that makes your character run fast, there is a short function where if you are currently running, it makes all parts called WaterPart in a model called WaterParts CanCollide true, whereas if you are not running it makes them CanCollide false. There are 47 of these parts in the map. This allows the character to run on water.

However, when your character starts or stops running, and these properties change, there is a short second-long lag. This is insanely annoying and rather ruins the experience, as every time you start or stop moving you experience a stutter. Strangely enough, this only started happening a day ago, whereas the function has been there since the code was written.

I and another scripter currently cannot think of any way to solve this problem, and have currently removed the function altogether. The code is included below, and we would appreciate any help on it.

spawn(function()
	while wait(.5) do
		if character.Humanoid.MoveDirection ~= Vector3.new(0,0,0) and running == true and character.Humanoid.WalkSpeed >= 50 then
			camShake:Shake(CamShake.Presets.Earthquake) -- irrelevant to this issue
			camShake:Start() -- irrelevant to this issue
			for _, v in pairs(workspace.WaterParts:GetChildren()) do
				if v:IsA("BasePart") and v.Name == "WaterPart" then
					v.CanCollide = true
				end
			end
		else
			camShake:Stop() -- irrelevant to this issue
			for _, v in pairs(workspace.WaterParts:GetChildren()) do
				if v:IsA("BasePart") and v.Name == "WaterPart" then
					v.CanCollide = false
				end
			end
		end
	end
end)

Try putting a Wait() in after v.CanCollide = false/true. This might help reduce lag.

1 Like

Thanks, that’s certainly got rid of the stutter. The downside is that it now takes 1.41 seconds for all the parts to change to CanCollide true/false, but I suppose I can live with it.

1 Like