Make bullets follow mouse

Hello, I present with an issue. I am trying to get the bullets to follow the mouse , but this wont work.
In the client script, it fires the server with information about the guns positon and the mouse position. And then in the server it starts calculating it, and makes it happen. Issue is, this isn’t working. Here is the error;

attempt to index a number value

Now here is the area where the server calculates;

function RandomVectorOffsetZoom(v, maxAngle) --returns uniformly-distributed random unit vector no more than maxAngle radians away from v
    return (CFrame.new(Vector3.new(), v)*CFrame.Angles(0, 0, rng_v:NextNumber(0, 1.5*math.pi))*CFrame.Angles(math.acos(rng_v:NextNumber(math.cos(maxAngle), 1)), 0, 0)).LookVector
end
--Calculations
local extendedRay = Ray.new((gunTip.CFrame.p-mousehit.CFrame.p).magnitude, RandomVectorOffsetZoom(gunTip.CFrame.lookVector, math.rad(5)) * (range))

Here is client side;

	local mousepos = mouse.Hit
	timeTillNextShot = fireGun:InvokeServer("GUNNAME", gun, fireCount,mousepos)

If anyone knows how to fix this error, it would be appreciated!

3 Likes

Can you please get a screenshot of your output.

Recoded some areas,
Extended ray new

		local extendedRay = Ray.new((gunTip.CFrame.p-mousehit).magnitude, RandomVectorOffsetZoom(gunTip.CFrame.lookVector, math.rad(5)) * (range))

Error is in server side script by the way, here is the new error. After my changes

 bad argument #1 (Vector3 expected, got number)

And the localscript

local mousepos = mouse.Hit.p
timeTillNextShot = fireGun:InvokeServer("(GUN)", gun, fireCount, mousepos)

Magnitude returns a number, and Rays are created with two Vector3s.

As the script output error says, the first arguments is an error. It expects a vector3 and got a number.
The part that I qouted above shows the error you did, you’re trying to subtract a vector3 value with a number value,

Correct way:

gunTip.CFrame.Position - mousehit.CFrame.Position

Way you did:

gunTip.CFrame.Position - mousehit

This did not work. If you read;
local mousepos = mouse.Hit.p
When I tried making it mouse.Hit.CFrame.p , this still didn’t work. This is an invalid solution.

Okay, read it wrong.
.magnitude returns a numbervalue
Consider doing something like this instead:

Ray.new(Vector3.new((gunTip.CFrame.Position.X - mousehit.X).magnitude, (gunTip.CFrame.Position.Y - mousehit.Y).magnitude, (gunTip.CFrame.Position.Z - mousehit.Z).magnitude)

try that instead, haven’t tried it myself!

1 Like