I have this problem with a body gyro. The expected behavior is, whenever I press shift, it zooms, and the BodyGyro turns my character to face the Mouse.Hit. However, this only works the first time. After resetting the character or calling :LoadCharacter() on it, it stops working.
This is in a Local Script
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RotatePart = Character:WaitForChild("Rotation")
local FaceMouse = RotatePart:WaitForChild("FaceMouse")
local Humanoid = Character:WaitForChild("Humanoid")
local PlayerGui = Player:WaitForChild("PlayerGui")
local Mouse = Player:GetMouse()
local GameSettings = UserSettings():GetService("UserGameSettings")
local RunService = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Camera = game.Workspace.CurrentCamera
local Tool = script.Parent
local ToolGui = Tool:WaitForChild("BlasterToolGui")
local Handle = Tool.Handle
local MuzzleAttachment = Handle.FirePosition
local FireDirection = Handle.FireDirection
local CameraPosition = Tool:WaitForChild("CameraPosition")
local equipped = false
local Zooming = false
print("Made new tool")
local function FaceMouseFunction()
if Player.Settings.AimWithMouse.Value == true then
FaceMouse.MaxTorque = Vector3.new(5000, 5000, 5000)
local NewCFrame = CFrame.new(Character.Rotation.Position, Mouse.Hit.p)
Player.Character.Rotation.FaceMouse.CFrame = NewCFrame
print("Changed body gyro, MaxTorque: " .. tostring(FaceMouse.MaxTorque) .. " Mouse.Hit: " .. tostring(Mouse.Hit.p))
else
FaceMouse.MaxTorque = Vector3.new(0, 0, 0)
end
end
local function UpdateCamera()
Camera.CFrame = CameraPosition.CFrame
end
local function LockCamera()
UserInput.MouseBehavior = Enum.MouseBehavior.LockCenter
Camera.CameraType = Enum.CameraType.Scriptable
RunService:BindToRenderStep("UpdateCamera", 199, UpdateCamera)
end
local function ReleaseCamera()
Camera.CameraType = Enum.CameraType.Custom
UserInput.MouseBehavior = Enum.MouseBehavior.Default
RunService:UnbindFromRenderStep("UpdateCamera")
end
local function ZoomIn()
if equipped == true and Camera.CameraType ~= Enum.CameraType.Watch and Zooming == false then
LockCamera()
Camera.FieldOfView = 40
Zooming = true
RunService:BindToRenderStep("FaceMouse", 199, FaceMouseFunction)
end
end
local function ZoomOut()
if equipped == true and Camera.CameraType ~= Enum.CameraType.Watch and Zooming == true then
ReleaseCamera()
Camera.FieldOfView = 70
Zooming = false
RunService:UnbindFromRenderStep("FaceMouse")
end
end
local function ToggleZoom(actionName, inputState, inputObject)
if equipped == true and inputState == Enum.UserInputState.Begin then
if Zooming == false then
ZoomIn()
else
ZoomOut()
end
end
end
Tool.Equipped:Connect(function()
ContextActionService:BindAction("ToggleZoom", ToggleZoom, true, Enum.KeyCode.LeftShift)
ContextActionService:SetTitle("ToggleZoom", "Shift")
ContextActionService:SetPosition("ToggleZoom", UDim2.new(0.5, 100, 0.5, 0))
equipped = true
ToolGui.Parent = PlayerGui
end)
Tool.Unequipped:Connect(function()
ContextActionService:UnbindAction("ToggleZoom")
ReleaseCamera()
equipped = false
ToolGui.Parent = Tool
if Camera.FieldOfView ~= 70 then
Camera.FieldOfView = 70
end
end)
LockCamera()
ReleaseCamera()
The function that controls the BodyGyro are:
local function FaceMouseFunction()
if Player.Settings.AimWithMouse.Value == true then
FaceMouse.MaxTorque = Vector3.new(5000, 5000, 5000)
local NewCFrame = CFrame.new(Character.Rotation.Position, Mouse.Hit.p)
Player.Character.Rotation.FaceMouse.CFrame = NewCFrame
print("Changed body gyro, MaxTorque: " .. tostring(FaceMouse.MaxTorque) .. " Mouse.Hit: " .. tostring(Mouse.Hit.p))
else
FaceMouse.MaxTorque = Vector3.new(0, 0, 0)
end
end
All the print statements work exactly as intended. The MaxTorque is correct.
Anyone have any idea how to solve this?