Ok, so I have a thirdperson script for a Gun, and i want whenever the gun is equipped to go into third person, and it does that but, it will only switch on MouseButton1 and idk how to fix it. I’ll leave code here and an gif of what happens.
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local cameraOffset = Vector3.new(2,2,8)
local mouse = Player:GetMouse()
Player.CharacterAdded:connect(function(char)
local humanoid = char:WaitForChild("Humanoid")
local rootPart = char:WaitForChild("HumanoidRootPart")
local cameraAngleX = 0
local cameraAngleY = 0
humanoid.AutoRotate = false
local function playerInput(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Change then
cameraAngleX -= inputObject.Delta.X
cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
end
end
ContextActionService:BindAction("PlayerInput",playerInput,false,Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
if script.Parent.Parent == char then
local startCFrame = CFrame.new(rootPart.CFrame.Position)*CFrame.Angles(0,math.rad(cameraAngleX),0) * CFrame.Angles(math.rad(cameraAngleY),0,0)
local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraAngleX,cameraOffset.Y, -100000))
camera.CFrame = CFrame.lookAt(cameraCFrame,cameraFocus)
local lookingCFrame = CFrame.lookAt(rootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0,0,-100000)))
rootPart.CFrame = CFrame.fromMatrix(rootPart.Position,lookingCFrame.XVector,rootPart.CFrame.YVector)
end
end)
end)
local function focusControl(actionName, inputState, inputObject)
if script.Parent.Parent:FindFirstChild("Humanoid") then
if inputState == Enum.UserInputState.Begin then
camera.CameraType = Enum.CameraType.Scriptable
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
--UserInputService.MouseIconEnabled = false
mouse.Icon = "rbxassetid://8230755404"
end
else
camera.CameraType = Enum.CameraType.Custom
mouse.Icon = [[]]
Player.Character.Humanoid.AutoRotate = true
end
end-- string name function add touch button inputTypes
ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)