Low fps when rotating turret

Hello everyone, im having some trouble with rotating my turret with the new method(CFrame:fromMatrix). When I start rotating the turret my fpd go down to like 2 or 3.
These are the two scripts I am using.

The one withh the loop that fires a remote event:

local model = game.Workspace.AmericanBase.Cannons["Anti-Air1"]
local SG = script.Parent.UI
local value = SG.BulletsLeft
local label = SG.TextLabel
local mesh = model.MeshPart
local seat = model.Seat
local player = game.Players.LocalPlayer
local event = model.Rotate
-----------------------------------------------------------------------------

print(player.Name)
local mouse = player:GetMouse()
print("Got the mouse")
while wait(0.05) do
	print("Loop started")
	if seat.Occupant ~= nil then
		print("First if")
		mouse.Icon = "http://www.roblox.com/asset/?id=138865017"
		local pos = mouse.Hit.Position
		print("Value true")
		event:FireServer(pos)
	else
		mouse.Icon = ""
	end
end

The one that receives the remote event and changes the position of the turret.

local event = script.Parent.Rotate
local mesh = script.Parent.MeshPart

local function getCFrame(position, lookAt)
	
    local lookVector = (position - lookAt).Unit 
    local modelUpVector = Vector3.new(0, 1, 0)
    local rightVector = lookVector:Cross(modelUpVector)
    local upVector = rightVector:Cross(lookVector)

	return CFrame.fromMatrix(position, rightVector, upVector)
end

event.OnServerEvent:Connect(function(plr, pos)
	print("Fired")
	mesh.CFrame = getCFrame(mesh.Position, -pos)
end)
	
3 Likes

This seems like an odd Issue, are you sure its the turret that is causing the lag?

1 Like

Yes, its a turret on which you sit and control it, when I sit it starts lagging.

Im pretty sure its because of the while wait(0.005) since doing a very low ammount of time to wait is a bad thing, Since thats in a local script you could do this:

local runService = game:GetService("RunService")
runService.Heartbeat:Connect(function()
       --Put everything you did put in the while wait(0.05) there
end)
1 Like

So now I have this but it still lags

local model = game.Workspace.AmericanBase.Cannons["Anti-Air1"]
local SG = script.Parent.UI
local value = SG.BulletsLeft
local label = SG.TextLabel
local mesh = model.MeshPart
local seat = model.Seat
local player = game.Players.LocalPlayer
local event = model.Rotate
local runService = game:GetService("RunService")
-----------------------------------------------------------------------------

print(player.Name)
local mouse = player:GetMouse()
print("Got the mouse")
runService.Heartbeat:Connect(function()
	print("Loop started")
	if seat.Occupant ~= nil then
		print("First if")
		mouse.Icon = "http://www.roblox.com/asset/?id=138865017"
		local pos = mouse.Hit.Position
		print("Value true")
		event:FireServer(pos)
	else
		mouse.Icon = ""
	end
end)

I update every 1/120 of a second from the client, also are there any humanoids in the workspace by any chance?

What do you mean?

30 charsssssss

Actually there is other solution for this, You could use the Seated event of the humanoid (So I would recommend putting the local script at starter character scripts)

After you get the Humanoid you use the Seated event , so basically this:

local character = script.Parent
local hum = character:WaitForChild("Humanoid")
local runServ = game:GetService("RunService")
local theConnection
hum.Seated:Connect(function(isSeated, theSeat)
       if isSeated and theSeat == seat then
             theConnection = runServ.Heartbeat:Connect(function()
                    --Code inside while wait(0.005)
             end)
       else
             if theConnection then
                theConnection:Disconnect()
             end
       end
end)

One thing too, for some reason, when I used the old deprecated method(CFrame.new(pos, lookat) everything worked fine.

1 Like

Look at script Performance (Open from View => Script Performance) and see what script is actually lagging, and if it is the script changing the turret’s CFrame and using the old CFrame.new(vector3,vector3) does work better, you might consider using that. I am aware it is “deprecated” however this thread shows some light on it and gives some better alternatives than what you are currently using: Is CFrame.new(Vector3 position, Vector3 lookAt) actually deprecated?
@AstralBlu_e, is smart to use a runservice connection, as wait() can sometimes yield for minutes when you call it too frequently, however remember to use the deltaTime argument like this:

RunService.RenderStepped:Connect(function(dt)
	local scale = dt*60
	Position = Position + speed * scale
end)

Also, rotating on the client is often a better idea than sending the server a position to rotate to, as that reduces input lag and network usage. In case you didn’t know, you can allow it to be replicated (as long as its not anchored) with BasePart:SetNetworkOwner. If it is anchored, you can consider using constraints (welds, motors, etc) and making the turret stay together while being unanchored.

1 Like

I used the script performance and this appears in the script that fires the event inside a loopimage

That script:

local model = game.Workspace.AmericanBase.Cannons["Anti-Air1"]
local SG = script.Parent.UI
local value = SG.BulletsLeft
local label = SG.TextLabel
local mesh = model.MeshPart
local seat = model.Seat
local player = game.Players.LocalPlayer
local event = model.Rotate
local runService = game:GetService("RunService")
-----------------------------------------------------------------------------

print(player.Name)
local mouse = player:GetMouse()
print("Got the mouse")
while seat.Occupant ~= nil and wait(0.05) do
	print("Loop started")
	if seat.Occupant ~= nil then
		print("First if")
		mouse.Icon = "http://www.roblox.com/asset/?id=138865017"
		local pos = mouse.Hit.Position
		print("Value true")
		event:FireServer(pos)
	else
		mouse.Icon = ""
	end
end