Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My Shoulder Camera Script Is Only Working On Studio Mode How Do I Fix That?

Asked by 4 years ago

So i got the shoulder Camera Script That Is In StarterPlayerScripts And I Got The Script From The Camera manipulation Article And When I Run It On Studio It Works Perfectly But when i'm in game It Does Not Work I have The Script Here: i checked the output for any errors but nothing appears.

local Players = game:GetService("Players") 
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Toggle = true
local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()

local camera = workspace.CurrentCamera
local cameraOffset = Vector3.new(2, 2, 8)
local player = Players.LocalPlayer

print(script.Parent, script.Parent.Parent)

player.CharacterAdded:Connect(function(character)

    local humanoid = character:WaitForChild("Humanoid")
    local rootPart = character:WaitForChild("HumanoidRootPart")
    humanoid.AutoRotate = false

    local cameraAngleX = 0
    local cameraAngleY = 0

    local function playerInput(actionName, inputState, inputObject)
        -- Calculate camera/player rotation on input change
        if inputState == Enum.UserInputState.Change then
            cameraAngleX = cameraAngleX - inputObject.Delta.X
            -- Reduce vertical mouse/touch sensitivity and clamp vertical axis
            cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
            -- Rotate root part CFrame by X delta
            rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
        end
    end
    ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

    RunService.RenderStepped:Connect(function()
        rootPart.CFrame = CFrame.new(rootPart.Position,rootPart.Position+camera.CFrame.lookVector*Vector3.new(1,0,1))
        if camera.CameraType ~= Enum.CameraType.Scriptable then
            camera.CameraType = Enum.CameraType.Scriptable
        end
        local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
        local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
        local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
        camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
    end)
end)

local function focusControl(actionName, inputState, inputObject)
    -- Lock and hide mouse icon on input began
    if inputState == Enum.UserInputState.Begin then
        UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
        UserInputService.MouseIconEnabled = false
        ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
    end
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enum.KeyCode.M then
        if Toggle == true then
            Toggle = false
            UserInputService.MouseBehavior = Enum.MouseBehavior.Default
            UserInputService.MouseIconEnabled = true
            controls:Disable()
        else
            Toggle = true
            UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
            UserInputService.MouseIconEnabled = false
            controls:Enable()
        end
    end
end)

ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

Can Anyone Help Me With This Issue Thanks! (English Is My Second language so its a bit bad)

Answer this question