Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this script allow the player to move right?

I have this script for a side scroller that lets the player move right

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 10
local cameraSpeed = .25
 
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')
 
local function setupCamera()
	camera.CoordinateFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset),
										Vector3.new(0,cameraHeight,0))
end
setupCamera()
player.CharacterAdded:connect(setupCamera)
 
local function onUpdate()
	if player.Character and player.Character:FindFirstChild('Torso') then
		local playerX = player.Character.Torso.Position.X
		local cameraX = camera.CoordinateFrame.p.X
 
		if cameraX - cameraXChase < playerX then
			camera.CoordinateFrame = camera.CoordinateFrame + Vector3.new(cameraSpeed, 0, 0)
		end
	end
end
 
RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)

The only problem is that it dosnt let the player go left. How do I fix that?

3 comments

0

Where in your code does it move the player to the left??

0

it dosnt "let" the move to the left, the player can already move tot he left as they please, but the camera will only follow them left. It just sorta stays dormant when they go to the right

0

oh

1 answer

✓ Accepted answer

The problem in your code was you were only checking one direction not both.

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 10
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local function setupCamera()
    camera.CoordinateFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset),
                                        Vector3.new(0,cameraHeight,0))
end
setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
    if player.Character and player.Character:FindFirstChild('Torso') then
        local playerX = player.Character.Torso.Position.X
        local cameraX = camera.CoordinateFrame.p.X

        if cameraX - cameraXChase < playerX then
            camera.CoordinateFrame = camera.CoordinateFrame + Vector3.new(cameraSpeed, 0, 0)
		elseif cameraX - cameraXChase > playerX then -- Adding this here allows for it to check the other direction. All I did was reverse what the if did.
            camera.CoordinateFrame = camera.CoordinateFrame - Vector3.new(cameraSpeed, 0, 0)
        end
    end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)

Hope it helps! If this helped please accept this answer so other people can see the solution.

2 comments

0

It worked! Thanks a bunch!

0

No problem. If you have any other questions feel free to message me and ask!

This discussion is preserved as it was. New answers, comments, and votes are closed.