Why wont this camera script work?

Please forgive me if I mess up or did not make anything clear enough. I Have this camera script, Roblox’s camera script from one of they’re templates, and i tried to make it so when you step on a part, the script runs and do what it should normally do;

https://gyazo.com/c22ff35c3f9df9639e281187f2376c17

But it doesn’t. Here’s the script:

        function onTouched(hit)
    	
    	local camera = game.Workspace.CurrentCamera
    local player = game.Players.LocalPlayer

    camera.CameraType = Enum.CameraType.Scriptable

    local targetDistance = 30
    local cameraDistance = -30
    local cameraDirection = Vector3.new(-1,0,0)

    local currentTarget = cameraDirection*targetDistance
    local currentPosition = cameraDirection*cameraDistance

    game:GetService("RunService").RenderStepped:connect(function()
    	local character = player.Character
    	if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
    		local torso = character.HumanoidRootPart
    		camera.Focus = torso.CFrame
    		if torso:FindFirstChild("FastStart") == nil then
    			camera.CoordinateFrame = 	CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y + 10, torso.Position.Z - 20) + currentPosition, 
    										Vector3.new(torso.Position.X,  torso.Position.Y, torso.Position.Z - 20) + currentTarget)
    		else
    			
    			camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentPosition, 
    											    Vector3.new(torso.Position.X,  torso.Position.Y - 15, torso.Position.Z - 20) + currentTarget)
    		end
    	end
    end)


    script.Parent.Touched:connect(onTouched)

    end

(also if anyone knows a better way to go about doing this, please tell :wink:)

Thanks for reading!

1 Like

I could be wrong, but I’m pretty sure camera.CoordinateFrame is deprecated? You should be using camera.CFrame. I checked camera properties on the developer hub and I don’t see CoordinateFrame listed so that’s why I figured it’s probably deprecated…

1 Like

Not sure if this is too late or the solution has been found but, I thought I’d post this anyway.

The main reason why your code didn’t work as intended is because script.Parent.Touched:connect(onTouched) is in your .Touched method. Connections should always be outside the scope of the callback method. It’s also worth noting that local scripts will not run in workspace or whose top-most parent is in workspace.

Secondly, Camera.CoordinateFrame has been removed in favor of Camera.CFrame. Both are identical and functionality is relatively the same across.

Since you’re invoking .onTouched every time .Touched is fired, RunService.RenderStepped:connect( ... ) will spawn the same number of time. You can correct this by your .RenderStepped connection as a variable and doing :disconnect() when invoked again. You could also, invoke it only once and set a boolean to perform different actions based on values.

Something like this:

local RenderedStep
RenderedStep = RunService.RenderStepped:connect( ... )
-- code
RenderedStep:Disconnect()

In addition, I had fun playing with this and simplified it.

local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Part = game.Workspace:WaitForChild("Part")

function onTouched(hit)
    Camera.CameraType = Enum.CameraType.Scriptable

    local targetDistance = 30
    local cameraDistance = -30
    local cameraDirection = Vector3.new(-1,0,0)

    local currentTarget = cameraDirection*targetDistance
    local currentPosition = cameraDirection*cameraDistance
	
	local offset = CFrame.new(-30,10,10)

    RunService.RenderStepped:connect(function()
    	if Character then
    		local RootPart = Character.HumanoidRootPart
    		Camera.Focus = RootPart.CFrame
    		if RootPart:FindFirstChild("FastStart") == nil then
				Camera.CFrame = CFrame.new((RootPart.Position+Vector3.new(0,10,-20))+currentPosition, (RootPart.Position+Vector3.new(0,0,-20))+currentTarget)
    		else
				Camera.CFrame = CFrame.new((RootPart.Position+Vector3.new(0,-15,-20))+currentPosition, (RootPart.Position+Vector3.new(0,-15,-20))+currentTarget)
    		end
    	end
    end)
end

Part.Touched:Connect(onTouched)

Here’s a place file if you want to use it for reference: CamTest.rbxl (21.9 KB)

Hope this helps!

2 Likes