#My script for my gun gets an error when I make the local direction variable.

1 messages · Page 1 of 1 (latest)

muted bobcat
#

I was following a tutorial about raycast and in one part of the tutorial we make a gun for some reason I get an error "attempt to perform arithmetic (sub) on vector3 and instance." the error happens in local direction.

#

the big one is the server script where the error happens and the small one is the client script I don't think there is any error there but maybe

mystic turtleBOT
#

studio** You are now Level 5! **studio

west plover
#

Which line gives the error?

muted bobcat
#

the local direction

#

local direction = (mousepos - origin).Unit * dist_mult

#

in the server script

muted bobcat
west plover
#

Okay sk

#

.unit returns a vector3

#

But you do multi * vector3

#

Mabey try vector3.new(1,1,multi) * (balh).unit

#

Cant multiply a number with vector3

#

So turn number into vector3

muted bobcat
#

I will try

muted bobcat
west plover
#

Blah* just the stuff you put ib

#

Im on mobile so i can only explain your fix

muted bobcat
#

sorry I don't know much about these stuff but what is ib?

copper quartz
#

local tool = script.Parent
local pointpart = tool:WaitForChild("Handle"):WaitForChild("Point")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local firegun = tool:WaitForChild("firegun")

tool.Activated:Connect(function()
firegun:FireServer(pointpart.Position, mouse.Hit.Position)
end) put this in the tool in a local script

muted bobcat
#

I did

copper quartz
#

local scriot

muted bobcat
#

yeah

copper quartz
#

script

#

local tool = script.Parent
local firegun = tool:WaitForChild("firegun")

local dist_mult = 50
local excludeparts = {}

local function visualizeray(origin, direction)
local length = direction.Magnitude
local midpoint = origin + direction * 0.5

local newpart = Instance.new("Part")
newpart.Anchored = true
newpart.CanCollide = false
newpart.Material = Enum.Material.Neon
newpart.Color = Color3.fromRGB(255, 148, 61)
newpart.Size = Vector3.new(0.1, 0.1, length)
newpart.CFrame = CFrame.new(midpoint, origin + direction)
newpart.Parent = workspace

game.Debris:AddItem(newpart, 0.5)

end

firegun.OnServerEvent:Connect(function(player, origin, mousepos)
local direction = (mousepos - origin).Unit * dist_mult

local raycastparams = RaycastParams.new()
raycastparams.FilterDescendantsInstances = excludeparts
raycastparams.FilterType = Enum.RaycastFilterType.Exclude

local rayresult = workspace:Raycast(origin, direction, raycastparams)

if rayresult then
    print("Result found!", rayresult)
    visualizeray(origin, direction)
    rayresult.Instance.Color = Color3.fromRGB(0, 255, 217) -- Changes hit part color
else
    print("Result not found!")
    visualizeray(origin, direction)
end

end)

#

this one too

#

in the tool loca script

#

did it work?

muted bobcat
#

is in a server script but I'm sure that is right

#

if is in a local script it will not affect the server

muted bobcat
copper quartz
#

did it work

muted bobcat
#

nope

copper quartz
#

The error you're seeing —
attempt to perform arithmetic (sub) on Vector3 and Instance — is happening because you're trying to subtract a Vector3 (a position) from an Instance (an object like a Part), which isn't valid.

Let’s look at the relevant part of your server script:

firegun.OnServerEvent:Connect(function(player, origin, mousepos)
local direction = (mousepos - origin).Unit * dist_mult

You're expecting both origin and mousepos to be Vector3 values (positions), but one of them isn't — likely origin.

From your local/client script, this is the part where you call FireServer:

firegun:FireServer(pointpart.Parent, mouse.Hit.Position)

You’re passing:

pointpart.Parent (which is a Model or Tool, i.e., an Instance) as origin

mouse.Hit.Position (which is a Vector3) as mousepos

You need to send a Vector3 position instead of pointpart.Parent.

#

try that

#

that is the problem that i see

#

you understand

muted bobcat
#

thank you I was wrong about the pointpart small script