Whille wait() do loops causing lag

Hello everyone, I am making a deployable ladder system where you point the mouse too. Im looping a part of the code that maked the ladder preview follow the mouse. For some reason, and this has started happening some days ago since it happens too with a turret I have that uses a similar system to point the mesh to the mouse, it causes lag.

This is the script I am using currently:

local tool = script.Parent
local previews = workspace.Previews
local one = previews.PStage1
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local equipped

tool.Equipped:Connect(function(mouse)
	print("Weapon equipped")
	equipped = true
	while wait(0.1) and equipped == true do
		print("Loop")
		one.CFrame = CFrame.new(mouse.Hit.Position)
		one.Transparency = 0.7
	end
end)

tool.Unequipped:Connect(function(mouse)
	print("Weapon unequipped")
	equipped = false
	one.Transparency = 1
end)

Thanks for reading, I hope there is a simple solution for this.

1 Like

You’re making a new loop every time you equip the weapon, it will lag when you start rapidly equipping enough since many loops will be running.

2 Likes

I only equip it one time and it lags.

1 Like

It shouldn’t, make sure you know only one is running. Also its bad practice to have a loop run in a event connected function anyways.

You have a redundant line where you set the transparency to 0.7, I recommend putting that before the loop in the function instead and it’ll save some resources.

1 Like

Still happens even if I remove the transparency

Check if your loop actually runs once and whether or not it is caused by that script alone, you can try a print call in the loop and then make sure it isn’t rapidly printing too much per 0.1 seconds.

I really can’t see anything wrong otherwise.

In addition, if you’re using the new console output in Studio, you may be experiencing lag.

2 Likes

Instead of using an equipped, make a mouse moved event instead of looping within an equipped event. That way lag is reduced by alot since it’s only changing when the mouse moves.

1 Like

In these cases I would advice against using while loops, although, as mentioned by @ArtFoundation you’re using these incorrectly as well. Read this to understand why you should avoid waits : Avoiding wait() and why) . Besides that, using UserInputService & InputChanged to detect the mouse movement would be a far better option since you’d be running the function when you actually need it.

4 Likes

To further help you out (although I want you to research this yourself as well), I’ll give you a small example code that you can apply to yours.

--//Make sure this is in a Local Script
local RunService = game:GetService("RunService")
local Tool       = script.Parent
local Equipped   = false
local Connection 

Tool.Equipped:Connect(function()
Equipped = true
     	Connection = UserInputService.InputChanged:Connect(function(Input, gameProcessed)
		 if Input.UserInputType == Enum.UserInputType.MouseMovement  then
			if gameProcessed then return end ---//This, simply said, is to ignore the input of your mouse if focused on UI
            if Equipped == false then Connection:Disconnect() return end ---//Disconnecting the connection if for some reason the .Unequipped event does not fire
		        ---//Do your function here 
               ---//print(Input) will print the position of the mouse on the screen for you in 2D, which you have to convert to 3D, I suggest reading this https://developer.roblox.com/en-us/api-reference/function/Camera/ScreenPointToRay
	     end
	    end)
end)

Tool.Unequipped:Connect(function()
Equipped = false
    if Connection then --//We're disconnecting the event so that we're not creating a new one each time we equip a tool
       Connection:Disconnect() 
    end
end)
1 Like

With my Experience With Loops the only time I Caused lag is when I printed Something too many times or Made the Wait less than .03. How I solve the Print is by clearing the output.

1 Like

Are you running this in studio when it lags? I have noticed that with the new beta feature for the output window (Expressive Output Window - Beta) studio lags significantly when printing things in the console quickly. If so, try disabling it in beta features.

3 Likes