How Do I Add Projectiles To Weapons?

Hello!

I have a gun that works perfectly fine; it shoots and it uses raycasting so that if the ray comes in contact with a player, it damages them.

The problem is that I’m very new to raycasting and I don’t fully understand how to use it/ how it works and I would like to make it so that a bullet is rapidly fired. Here is my script containing the raycasting:

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePosition)
	local Damage = script.Parent.Parent.Values.Damage -- How much damage the weapon does
	local FiredSound = script.Parent.Parent.Pistol.Pistol.Fired -- The sound that is played when fired
	
	local Handle = script.Parent.Parent.Handle -- Handle of the weapon (Where the bullet should ideally start)
	
    -- Play FireSound to all players in a 115 stud radius
	for Number, Value in pairs(game.Players:GetChildren()) do
		local magnitude = math.abs((Handle.Position - Value.Character.HumanoidRootPart.Position).Magnitude)
		
		if magnitude <= 115 then
			FiredSound:Play()
		end
	end
	
    -- Raycasting stuff
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(Handle.Position, (mousePosition - Handle.Position)*100, raycastParams)
	
	if raycastParams then
		if raycastResult then
			local hitPart = raycastResult.Instance
			local model = hitPart:FindFirstAncestorOfClass("Model")
			
			if model then
				if model:FindFirstChild("Humanoid") then
					model.Humanoid.Health -= Damage.Value
				end
			end
		end
	end
end)

Any help is appreciated, thank you in advance!! :smiley:

1 Like

Are you trying to make ‘projectiles’ (bullets) or are you trying to make the gun capable of rapid firing?

Yes I am trying to make bullets/ projectiles. :laughing:

You can Instance.new the bullet and make the bullet’s lookVector to the target. This will make the bullet face the target. Then you can do something like add velocity to the bullet. but if you’re looking for more advanced gun module. I’d suggest you look at FastCastRedux it’s an amazing module.

2 Likes