Anyways the script is in a localscript in the starterplayerscripts. The problem is when I call the step function without a wait(1) before it it freezes roblox. I am wondering why it causes this much lag and freezes my game. And how can I fix it so that I no longer have to wait(1). to be more specific it the problem is with line 12.

local plr = game.Players.LocalPlayer
while not plr.Character do wait() end
local char = plr.Character
char:WaitForChild("Humanoid")
char:WaitForChild("Head")
local torso = char:WaitForChild("Torso")

function step()
	local cframe = CFrame.new(char:FindFirstChild("Head").Position, mouse.Hit.p)
	local cfYaw = CFrame.new(Vector3.new(),(cframe.lookVector * Vector3.new(1, 0, 1)).unit)
	stableG.cframe = CFrame.new(char:FindFirstChild("Torso").Position) * cfYaw 
	char.Torso.CFrame = CFrame.new(char:FindFirstChild("Torso").Position) * cfYaw --the problem is here
end

game:GetService("RunService").Heartbeat:connect(function()
if game:GetService("UserInputService").TouchEnabled ~= true and char:FindFirstChild("Head") then
	step()
end
end)

3 comments

0

You have to put wait (0.5). I don't think there's another way of preventing that.

0

Try to put an empty "wait()"

0

sure that works kind of but sometimes it will still error. it doesn't fix the problem.