Need help making a gun with raycasting

Hello, I’m trying to make a basic gun using raycasting with projectiles that will damage the player. I know how to create the ray and make a bullet once the tool is activated, but how would I make the bullet spawn from the tool and make the bullet move to the mouse position using linear velocity? If linear velocity isn’t a good option, what better ways are there to move the bullet to the target?

Any help or suggestions would be greatly appreciated.

local rs = game:GetService("ReplicatedStorage")

local remoteEvent = rs:WaitForChild("BlasterActivated")
local tool = game:GetService("StarterPack"):WaitForChild("Blaster")
local bullet = rs:WaitForChild("Bullet")

remoteEvent.OnServerEvent:Connect(function(player, mousePosition)
	print("Server fired!")
	local bulletSpeed = 200
	local direction = (mousePosition - tool.Handle.Position).Unit * bulletSpeed
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {tool, player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true
	local raycast = workspace:Raycast(tool.Handle.Position, direction, raycastParams)
	
	local newBullet = bullet:Clone()
	local bulletFolder = Instance.new("Folder", game.Workspace)
	bulletFolder.Name = "BulletsFolder"
--The bullet isn't spawning at the tool handle so I'm guessing there's also a problem here
	newBullet.CFrame = CFrame.new(tool.Handle.Position, mousePosition)
	
	local linearVelocity = Instance.new("LinearVelocity", newBullet)
	local att0 = Instance.new("Attachment", newBullet)
	linearVelocity.Attachment0 = att0
	linearVelocity.LineVelocity = --idk what I'm doing here
	newBullet.Parent = bulletFolder
	
end)```

Certainly! Let’s address your request for creating a basic gun using raycasting with projectiles. I’ll guide you through the process of spawning bullets from the tool and making them move toward the mouse position.

  1. Spawning Bullets from the Tool:
    To spawn bullets from the tool, you’re already on the right track by cloning the bullet object. However, let’s ensure that the new bullet is positioned correctly relative to the tool handle. You mentioned that the bullet isn’t spawning at the tool handle, so let’s fix that.

  2. Setting the Bullet’s Position:
    Instead of setting the bullet’s CFrame directly, you can use the Position property to place it at the tool handle’s position. Here’s the modified code snippet:

    local newBullet = bullet:Clone()
    newBullet.Position = tool.Handle.Position
    newBullet.Parent = bulletFolder
    
  3. Moving the Bullet:
    To make the bullet move toward the mouse position, you can use linear velocity. However, there’s a small issue in your code where you left the linearVelocity.LineVelocity value empty. Let’s calculate the correct velocity vector based on the direction toward the mouse:

    local bulletSpeed = 200
    local velocity = direction * bulletSpeed
    linearVelocity.Velocity = velocity
    
  4. Alternative Approaches:
    While linear velocity works well for simple projectiles, you might also consider using BodyVelocity or BodyForce for more complex behavior. These allow you to apply forces directly to the bullet part.

  5. Final Code:
    Here’s the adjusted code snippet:

    local rs = game:GetService("ReplicatedStorage")
    local remoteEvent = rs:WaitForChild("BlasterActivated")
    local tool = game:GetService("StarterPack"):WaitForChild("Blaster")
    local bullet = rs:WaitForChild("Bullet")
    
    remoteEvent.OnServerEvent:Connect(function(player, mousePosition)
        print("Server fired!")
    
        local bulletSpeed = 200
        local direction = (mousePosition - tool.Handle.Position).Unit
        local velocity = direction * bulletSpeed
    
        local newBullet = bullet:Clone()
        newBullet.Position = tool.Handle.Position
        newBullet.Parent = bulletFolder
    
        local linearVelocity = Instance.new("LinearVelocity", newBullet)
        linearVelocity.Velocity = velocity
    
        -- You can also add collision detection logic here if needed
    end)
    

Feel free to ask if you need further assistance or have any other questions!

1 Like

I feel like you should just use the fastcastredux module, and they link an example tool in it as well you can just tweak.

1 Like

Hello, I appreciate the help! After making some tweaks from what you mentioned, the bullet now does follow the mouse position but it doesn’t spawn at the handle even when using the position property. The bullet spawns at the same location everytime. In the properties tab, the handle position stays the same even though the handle clearly isn’t at the position it says. Is there another property to control the position of the handle that would fix this?

Also in regard to your suggestions about BodyVelocity and BodyForce, aren’t they both deprecated now? Should they still be practiced? On the documentations it says you should use linear velocity and vector force.

1 Like

I’m still really new to scripting and haven’t really looked into module scripts just yet. Maybe I’ll give it a look soon I’m just still learning the basics and how to apply them.

I’m glad to hear that the bullet is now following the mouse position! Let’s address the issue with the bullet not spawning at the handle position correctly.

  1. Handle Position:

    • The handle position you’re using might not accurately represent the actual position of the handle.
    • Instead of relying solely on the tool.Handle.Position, consider using a more reliable reference point.
  2. Alternative Approach:

    • To ensure that the bullet spawns at the correct location, you can use a different reference point, such as the tool itself or another part within the tool.
    • For example, if you have a part named BulletSpawnPoint within your tool, use its position as the spawn location.
  3. Example (Using a Custom Spawn Point):

    local bulletSpawnPoint = tool:FindFirstChild("BulletSpawnPoint")
    if bulletSpawnPoint then
        local newBullet = bullet:Clone()
        newBullet.Position = bulletSpawnPoint.Position
        newBullet.Parent = bulletFolder
    
        local linearVelocity = Instance.new("LinearVelocity", newBullet)
        linearVelocity.Velocity = velocity
    else
        warn("BulletSpawnPoint not found in the tool.")
    end
    
  4. Regarding BodyVelocity and BodyForce:

    • You’re correct that both BodyVelocity and BodyForce are deprecated.
    • The recommended alternatives are LinearVelocity and VectorForce.
    • Stick with LinearVelocity for applying velocity to the bullet, as you’ve done.

Remember to adjust the code according to your specific tool, and the reference point you choose. If you encounter any further issues or have additional questions, you know what to do.