Problem with moving an object when pressing a button

Local:

local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
local ui = game:GetService("UserInputService")
local currentCamera = workspace.GameCamera

wait(1)

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = workspace.GameCamera.CFrame

ui.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.A then
		currentCamera.Position = currentCamera.Position - 1
	end
end)

So I have a script, I want to click on A. The player’s camera moves slightly to the left, and when you press D to the right. Well, and maybe that when you click it will move until the player presses the button.

Except that this error appears:

Players.Dssall1.PlayerGui.GameGui.LocalScript:13: attempt to perform arithmetic (sub) on Vector3 and number - Client - LocalScript:13

2 Likes

You cant subtract a number from a Vector value. Instead you can do currentCamera.Position - Vector3.new(1, 1, 1) or currentCamera.Position - currentCamera.CFrame.RightVector

5 Likes

Basically, you need to perform arithmetic at the moment when both values have same type.

1 Like

Okay, thank you, but how do you do the same only when clamping? That is, as long as the player pressed the button camera moves.

exactly, just adding an information on your script for him
`Vector3.new(0,0,0) have 3 values its (xyz or x,y,z) so you cant just do “-1” like that cuz its not based in 1 value.

3 Likes

You will need the RunService for that.

Create a variable called ButtonADown and set it to true when the player pressed down the A key, and then set it to false when they no longer press it.

local MovementSpeed = 1 -- adjust this to make it move faster or slower

RunService.RenderStepped:Connect(function(DeltaTime)
	if ButtonADown then
		currentCamera.Position - (currentCamera.CFrame.RightVector * MovementSpeed * DeltaTime)
		-- it has to be multiplied with DeltaTime to make sure the move speed does NOT depend on the framerate
	end
end)

You can do this for every direction, just make sure to use the right directions.
A is - RightVector, D is + RightVector, W is + LookVector and S is - LookVector

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.