7: bad argument #2 (Vector3 expected, got CFrame)

In island royale, there is a custom camera, type scriptable. When you move your mouse, your camera slightly moves towards it, I am trying to replicate this, but this isn’t working. I got another script that already changes the camera CFrame, so I made a lc in startergui and this error on line 7 keeps happening;

7: bad argument #2 (Vector3 expected, got CFrame)

Here is the code;

camera.CameraSubject = workspace.CamPart
local mouse = game.Players.LocalPlayer:GetMouse()
local posmouse = mouse.Hit
while wait(.1) do
if camera.CameraSubject == workspace.CamPart then
	camera.CoordinateFrame = workspace.CamPart.Position -posmouse
	--posmouse-workspace.CamPart.Position
end
end
3 Likes

Is the error line referencing the line you commented out in your code?

	--posmouse-workspace.CamPart.Position

I counted down to line 7, and that was the comment.

Just tested, the line isn’t that commented one.

camera.CoordinateFrame = workspace.CamPart.Position -posmouse is the error.

Trying to fix now

2 Likes

You probably just have to flip around the operators then on that line:

camera.CFrame = posmouse - workspace.CamPart.Position

Vector3 - CFrame is not a valid operation, which is probably why it is erroring, since mouse.Hit returns a CFrame and not a Vector3.

4 Likes

you need to convert the CFrame to a Vector.
Vector3.new((posmouse- workspace.CamPart.Position).p)

Edit
replied to the wrong message
i read the post wrong. its cframe-vector3 not vector3-cframe

CoordinateFrame expects a CFrame!

Doesn’t work, now the camera is not even moving, it’s just pointing a different direction.

Original;

With @goldenstein64 's edit

local posmouse = mouse.Hit This is the root of your problem, this returns a CFrame but you’re asking for a Vector. Try mouse.Hit.Position instaid of mouse.Hit.

Another problem I see with your code is that camera.CoordinateFrame requires a CFrame, not a vector. Try workspace.CamPart.CFrame instaid of workspace.CamPart.Position.

Also make sure the part is facing in the right direction. Meaning the ‘Forward’ face of the part must be facing in the direction you want to face in.

Hopefully this solves everything!

2 Likes

When I changed the area you requested me to, a new error has birthed;

7: bad argument #3 (CFrame expected, got Vector3)

CFrames already have a position factored into their value, since it has a CFrame.Position property. The real problem is probably just that you want the direction the mouse is facing but not its position as well. You can just subtract its position, and it should give you what you want:

camera.CFrame = (posmouse - posmouse.Position) + workspace.CamPart.Position

Actually, this probably wouldn’t work either because it could cause your camera to spin from adding to the orientation every .1 seconds. That depends on your original camera script though.

You could probably use the delta between the camera’s current CFrame and mouse.Hit's orientation to get on the right track:

camera.CFrame = (camera.CFrame - camera.CFrame.Position):Inverse() 
	* (posmouse - posmouse.Position) 
	+ workspace.CamPart.Position

If that doesn’t work, switch around posmouse and camera.CFrame and see if that does it.

1 Like

Doesn’t work, here’s what I ended up with;

1 Like

What does your other script’s code like by the way, at least the part that’s affecting it? It’s most likely messing with the results.

local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
camera.CameraSubject = workspace.CamPart
camera.CoordinateFrame = workspace.CamPart.CFrame
camera.CameraType = Enum.CameraType.Scriptable
plr.Character:SetPrimaryPartCFrame(workspace.Union.CFrame)

That’s all it is, I don’t think it’s messing with any of it.

1 Like

The problem is the code tries to set the camera rotation to the mouses rotation. But when the camera rotates it also automaticly rotates the mouse.

Instaid of setting it to the mouses rotation each time, try to do newrotation = oldrotation + deltarotation.

You can use GetMouseDelta(). The problem with MouseDelta is that it returns the delta in pixels, you’ll need to change this to rotation.

local uiService = game:GetService("UserInputService")
local SCREEN_RES = workspace.CurrentCamera.ViewportSize
local sens = Vector2.new(2,2) --sensetivity
local starterCFrame = workspace.CamPart.CFrame

function render() --call this each frame, I would recommand: runService:BindToRenderStep()
	local cDelta = iuService:GetMouseDelta()
	if oDelta.Y == cDelta.Y then
		cDelta = Vector2.new(cDelta.X,0)
	end
	local yD = cDelta.Y/-SCREEN_RES.Y/2*sens.Y --delta into rotation
	if camRot <= -1.4 then
		camRot = -1.4
	elseif camRot >= 1.4 then
		camRot = 1.4
	end --set a max y rotation so the user can't just flip thier screen
	local xD = cDelta.X/-SCREEN_RES.X/2*sens.X
	camRot = camRot + Vector2.new(yD,xD)
	workspace.CurrentCamera.CFrame = starterCFrame*CFrame.Angles(camRot.X,camRot.Y,0)
end

Didn’t work, code has 2 declarations

Ah, how stupid of me! My mistake sorry, after workspace.CurrentCamera.CFrame = and before end put oDelta = cDelta and at the very beginning of the code, near starterCFrame put:

local camRot = Vector2.new() --stores all delta values
local oDelta = Vector2.new() --the previous delta, this is used to check if the mouse is still or not. There's a 'bug' with MouseDelta() that it when you stop moving the mouse it gives out old deltas.

Try increasing the values of sens.

1 Like

Still didn’t work. Not sure why, now the camera won’t even move.

And an error is born;

13: attempt to compare userdata with number

Yep, I’ve put camRot instaid of camRot.Y, it’s common for me to make these small mistakes. Replace all camRot with camRot.Y. Only replace it tho it there is nothing behind it. (aka camRot replace, camRot.X not)

Also, don’t forget to call render() each frame.

You could use a different method to achieve this effect, and it would actually work a little better.

Instead of checking the mouse’s hit position, you can alter the camera angle based on the mouse’s screen position. This would work across all resolutions and FieldOfViews, so I prefer to use it.

You get (Mouse.X/Mouse.ViewSizeX)-0.5, and this will give you a number between -0.5 and 0.5, and then do the same for the Y.
Once you have that, you can use CFrame.Angles() to alter the camera CFrame based on the mouse position.

local Player	= game.Players.LocalPlayer
local Mouse		= Player:GetMouse()
local Camera	= workspace.CurrentCamera

local CamCF		= workspace.CamPart.CFrame


game:GetService("RunService"):BindToRenderStep("CameraAngle", 201, function()
	local MousePos = Vector2.new((Mouse.X/Mouse.ViewSizeX)-0.5,(Mouse.Y/Mouse.ViewSizeY)-0.5)
	
	Camera.CFrame = CamCF*CFrame.Angles((-MousePos.Y/5),(-MousePos.X/5),0)
end)
6 Likes