Need help with CFrames

So I am currently trying to make bullet spread on a shotgun, but uh… the bullets just sort of do their own thing when RayCast.Direction > 0. I’ve tried to fix it in every way I could think of, at this point I’m just stumped and have no idea what to do.

Here is the code:

local Shots = 10;
for Index = 1, Shots do
    local OffsetDegrees = (((360 / Shots) * Index) + math.random(-36, 36));
    local NewRayTarget = ( * CFrame.Angles(math.rad(OffsetDegrees), 0, 0) * *, 0, 10));
    local FinalRay =, NewRayTarget.Position);
    BulletsReturn[Index] = Bullets.CreateBullet(Player, FinalRay);
RayCast =, Mouse.Hit.LookVector)

Here is a video demonstration of what I mean:


Your code has several errors. Working with Rays is more difficult than just using vectors. So instead of fixing it I decided to use vectors and from that make some changes to make it work properly.

	--local RayCast =, Mouse.Hit.LookVector)
	local origin = LocalPlayer.Character.Head.Position
	local target = Mouse.Hit.Position

	local Shots = 10;
	for Index = 1, Shots do
		local OffsetDegrees = math.random(-15, 15)
		local NewRayTarget = CFrame.lookAt(origin, target) * CFrame.Angles(0, 0, math.rad(math.random(-90, 90))) * CFrame.Angles(math.rad(OffsetDegrees), 0, 0)
		local FinalRay =, NewRayTarget.LookVector)
		BulletsReturn[Index] = Bullets.CreateBullet(Player, FinalRay)

You’re not allowed to do this.

He has been on the devforum since 2018, its just he didn’t do the intro thing. Now he has done it, it erased, how many posts he had read, how much time he has spent on devforums etc.

1 Like

Their join date doesn’t matter. They have to post for themselves, you can’t do this

Ok, well I’ll keep it in mind for next time.

It fixed the problem of direction, but all the bullets just go to 0, 0, 0.

It might be because it is hitting the bullet or the character try ignoring/ blacklisting it.


No, it is shooting towards 0, 0, 0.

I am talking about using my idea with your old code before using the code that @luya_yobanka told you.


Ok, I’ve tried that and it didn’t work :confused:

If I were you I would use the new ray cast and you can still get the ray cast hit position.


I already use the Hit.Position.

It’s because it doesn’t fly in the direction of the gun, try making it so it will fly in the direction of the gun. Try setting it to shoot at the barrels CFrame.


No, that’s the problem. It works perfectly in certain directions, but in others it just goes in random directions.

Try setting the barrels cancollide to false so it doesn’t mess with the bullets trajectory.


There is no gun yet, it comes from the character’s head based on your camera rotation. And no the character doesn’t mess with it.

@detroit_unspecific You can close the post now, because I have fixed the problem. Also thanks to @SloppyBanana225, @luya_yobanka, @rottendogDkR for your help, bits of your solutions combined fixed the problem. In the end the code turned out to be:

local OffsetDegrees = (((360 / BulletSplitMinimum) * Index) + math.random(-BulletSpread, BulletSpread));
local NewRayTarget = (, -RayCast.Direction) * CFrame.Angles(math.rad(math.random(-BulletSpread, BulletSpread)), math.rad(math.random(-BulletSpread, BulletSpread)), math.rad(OffsetDegrees)) *, 0, Range));
local FinalRay =, RayCast.Direction).Position, NewRayTarget.Position);
BulletsReturn[Index] = Bullets.CreateBullet(Player, FinalRay, Character);

Thanks, your prompt actually helped me fix a problem that came up later :slight_smile:
I realised that the two inputs to a were Origin and Direction, not Destination.


@TheSilverLeopard how did my reply help ?