How do I make a raycast shotgun?

I’d like to know how I can go about making a raycast shotgun, something like this, but of course with multiple bullets shot at once.

https://gyazo.com/c4a1b8e3e983e168efd6232282ce64c4

I have a gun system set up I just need to know how to do multiple bullets at a time, as I can’t think of any good methods to do this.

2 Likes

Just run a loop 1 through [max bullets], and run your code for shooting.

for SomeVariable=1,8 do		
    --do shooting stuff 
end

Something like that?

Yeah, but if you don’t need the variable name it _, as in _=1,8 (doesn’t change anything, just convention).

1 Like

It works, however each bullet has the same spread so it shows up as one bullet, when in reality it’s 8 different bullets occupying the same space. How do I make the bullet spread random for each individual bullet?

https://gyazo.com/2238a848ff6c8dd7e26e346ecd01616c

For each bullet, shoot the position at the mouse position + a random number for the X and Y axes.

2 Likes

How do I do it for each individual bullet here? Is there a method? I’m pretty new to Lua, sorry.

	local startPos	= localCharacter:WaitForChild('Head').CFrame.p
	local mousePos	= localMouse.Hit.p
	
	local spreadInterval = (newTool.Barrel.Position - mousePos).magnitude
	local Min, Max = -(gunConfig.bulletSpread/100) * spreadInterval, (gunConfig.bulletSpread/100) * spreadInterval
	local shotgunBullet = Vector3.new((mousePos.X)+(math.random(Min, Max)), (mousePos.Y)+(math.random(Min, Max)), (mousePos.Z)+(math.random(Min, Max)))
					
	local hitPart, hitPosition	= workspaceService:FindPartOnRay(Ray.new(startPos, (mousePos - startPos).unit * 999), localCharacter)
					

	for _= 1,8 do
		print('hi')
		renderLocalBullet({newTool.Barrel.Position, shotgunBullet, localPlayer.TeamColor})	
		remoteEvent:FireServer(securityKey, 'RENDERBULLET', {newTool.Barrel.Position, shotgunBullet, localPlayer.TeamColor})
	end

Move the declaration for shotgunBullet and your raycasting code inside the loop.

5 Likes

Works, thanks a lot!

2 Likes

@Kampfkarren I’m having issues where it shoots fine, but does no damage to the character, but when you go up close, it shoots one bullet and does some damage to the character. Any idea why?

Here is the code.

You’re calling return inside your loop, which stops the loop entirely. Move the code to a function and call the function from the loop. (By the way, your code is entirely insecure and a hacker can call your DAMAGEOBJECT from their client and kill everyone in the game, do your code on the server and not the client).

1 Like

Will do, and yes I just need this so it can be functional, there’s not many people who will bother doing it and those who do get banned easily because of the small server size of my group game.

After I move the code, the only thing in the for loop should be the function correct?

Also to clarify do I keep return inside the function or remove it?

Yeah, move it out of the loop and just run the function.

Works, there is one issue however.

https://gyazo.com/cc7c68a8154e56ab7687001f1a18e063
https://gyazo.com/4288ef0717e1598e14fe84dfc342fe30

The shotgun isn’t doing damage from further than 2-3 studs, and it also shows the weird behaviour above. Any idea why? I think each bullet isn’t doing damage, either it insta-kills, or does no damage, it’s being inconsistent.

Current code.

It may be because you’re looping through every R15 part in your loop through every R6 part, rather than being separate.

Maybe something a little more sophisticated than this, particularly if you are going to show where the pellets hit. Just adding random numbers to the aim point will give you a uniform distribution, over a square. What you want, typically, is a circular distribution with gaussian weighting or some approximation thereof.

5 Likes