Vector3 expected, got Instance

I was working on a FPS game using a framework tutorial with this link here.
I get an error when I try to shoot, here is my current module script:



local MainModule = {}

function MainModule.Update(viewmodel, dt)
	viewmodel.HumanoidRootPart.CFrame = workspace.Camera.CFrame
end

function MainModule.weldgun(gun)
	local Main = gun.GunComponets.Handle

	for i,v in ipairs(gun:GetDescendants()) do
		if v:IsA("BasePart") and v ~= Main then
			local newMotor = Instance.new("Motor6D")
			newMotor.Name = v.Name
			newMotor.Part0 = Main
			newMotor.Part1 = v
			newMotor.C0 = newMotor.Part0.CFrame:inverse() * newMotor.Part1.CFrame
			newMotor.Parent = Main
		end
	end
end

function MainModule.equip(viewmodel, gun, hold)
	local GunHandle = gun.GunComponets.Handle
	local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle

	gun.Parent = viewmodel
	HRP_Motor6D.Part1 = GunHandle

	local Hold = viewmodel.AnimationController:LoadAnimation(hold)
	Hold:Play()
end

function MainModule.cast(gun,endposition,velocity)
	local GunBarrel = gun.GunComponets:WaitForChild("Barrel")

	local Bullet = Instance.new("Part")
	Bullet.Size = Vector3.new(1,1,5)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.Color = Color3.new(255,255,255)
	Bullet.Material = Enum.Material.Neon
	Bullet.Parent = workspace

	Bullet.CFrame = CFrame.new(Bullet.Position, endposition)

	local Loop

	Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
		Bullet.CFrame *= CFrame.new(0,0 -velocity * (dt * 60))
		if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
			Bullet:Destroy()
			Loop:Disconnect()
		end
	end)
end


return MainModule

Here is the error message:

Screenshot 2021-05-04 143500

Here is that line of code:

Bullet.CFrame = CFrame.new(Bullet.Position, endposition)

I don’t know why this is happening, I started doing this hours ago, and I couldn’t find any answers.

Help is appreciated!

Can we see the cast function? It’s in a different script, as it’s expecting a Vector3 value but instead, got an Instance

There are 2 scripts, a local handler for the arms, and this module for all the functions, the error is in the module script, and so is the cast function. Here is the local handler because the problem might be in there(also you said so):

local GunModel = game.ReplicatedStorage:WaitForChild("SR01A1")
local AnimationsFolder = game.ReplicatedStorage:WaitForChild("SR01A1_ANIMS")
local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel")

local MainModule = require(game.ReplicatedStorage:WaitForChild("MainModule"))

ViewModel.Parent = workspace.Camera
MainModule.weldgun(GunModel)

game:GetService("RunService").RenderStepped:Connect(function(dt)
	MainModule.Update(ViewModel, dt)
end)

MainModule.equip(ViewModel,GunModel,AnimationsFolder.SR01A1_ANIM)

local IsPlayerHoldingMouse
local CanFire = true
local FireDelay = 0.1

game:GetService("RunService").Heartbeat:Connect(function(dt)
	if IsPlayerHoldingMouse then
		if CanFire then
			CanFire = false
			
			MainModule.cast(GunModel, game.Players.LocalPlayer:GetMouse(), 60)
			
			wait(FireDelay)
			CanFire = true
		end
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = false
	end
end)

Ooooh I see the issue, you only just passed the Local Player’s Mouse Object and not its actual Position lol

Try this:

local GunModel = game.ReplicatedStorage:WaitForChild("SR01A1")
local AnimationsFolder = game.ReplicatedStorage:WaitForChild("SR01A1_ANIMS")
local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel")
local Mouse = game.Players.LocalPlayer:GetMouse()

local MainModule = require(game.ReplicatedStorage:WaitForChild("MainModule"))

ViewModel.Parent = workspace.Camera
MainModule.weldgun(GunModel)

game:GetService("RunService").RenderStepped:Connect(function(dt)
	MainModule.Update(ViewModel, dt)
end)

MainModule.equip(ViewModel,GunModel,AnimationsFolder.SR01A1_ANIM)

local IsPlayerHoldingMouse
local CanFire = true
local FireDelay = 0.1

game:GetService("RunService").Heartbeat:Connect(function(dt)
	if IsPlayerHoldingMouse then
		if CanFire then
			CanFire = false
			
			MainModule.cast(GunModel, Mouse.Hit.Position, 60)
			
			wait(FireDelay)
			CanFire = true
		end
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = false
	end
end)

That fixed the first error, but know there is another error.
Screenshot 2021-05-04 184436

Here is the line of code:
Bullet.CFrame *= CFrame.new(0,0 -velocity * (dt * 60))

Try replacing that line

With this?

Bullet.CFrame *= CFrame.new(0,0 -velocity) * (dt * 60)

Same thing happened, the same error showed and nothing changed.

Wait I don’t even see a second comma what

Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
1 Like

It shoots fine, but the bullet is horrible displaced. They shoot in the direction of the mouse and angle fine, but the starting place is in the same place, not where the gun is.

Change Bullet.Position to reflect the barrel position on this line.

1 Like

what do you mean by reflect? How would I do that?

I believe the first CFrame parameter will take the position of where you want the Bullet to align its position from, so you have to set that where your current Gun’s Handle is, or:

Bullet.CFrame = CFrame.new(GunBarrel.Position, endposition)

And the second parameter would align where the bullet would rotate & aim towards relative to your Mouse’s Position

1 Like

It should not be CFrame.lookAt?

I don’t think it’d matter which way you would do it, plus I don’t believe CFrame.new(Pos, LookAt) isn’t deprecated but rather recommend to use the new CFrame.lookAt

I understand that, but how can I ‘reflect’ the position of the bullet to the barrel like @JarodOfOrbiter said

I’m pretty sure he meant “to reflect” as to give a reference point on where you want the Bullet to Position itself from

1 Like

Oh okay, how strange that CFrame.LookAt exists if CFrame.new can do it.

Alright good, but how can I do this? I could update the spawn position every frame if the position has moved? If that’s a good idea, how could I do it?

	local Loop

	Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
		Bullet.CFrame *= CFrame.new(0,0 -velocity * (dt * 60))
		if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
			Bullet:Destroy()
			Loop:Disconnect()
		end
	end)

Isn’t this what you want though? It’ll keep moving the position determined by how much Velocity you’ve assigned to the Bullet, and it’ll keep detecting the Position every frame using RenderStepped, and you implementing sanity checks detecting if the Part is far away enough you can destroy it & disconnect the Event

Quick question, how do I share a video without using 3rd party websites like youtube?