How to make a raycasting gun

Oh well thanks for your help though

2 Likes

Will this work for mobile devices?

3 Likes

Since it uses the peripheral target of the mouse, the object it is hovering over or put on, no. This only works for devices like a PC.

3 Likes

Could you be kind enough to guide me how to make it mobile friendly . . The cursor / tap to shot system from weapons kit game is what I hope to achieve . .

4 Likes

Construct a raycastparams, then set its FilterType to Enum.RaycastFilterType.Blacklist and add the character to the FilterDescendantsInstances.

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = { player.Character }

...
local result = Workspace:Raycast(origin, direction, params)
9 Likes

My friend hired me to script a gun and I’ll be definitely using this. However, this is only single shot, is there a way to make it automatic?

3 Likes

tool.Activated:Connect(function()
remote:FireServer(cursor.Hit.Position)
end)
Change this to make it so that everytime the mousebutton2 button is held down fire a bullet. This can be done using RunService.Heartbeat (for constant checking) and UserInputService:IsKeyDown() to check if the key is down.

And if it is down then tell the server to fire.

4 Likes

I recommend something like this

local TriggerPulled = false

UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.MouseButton1 then
TriggerPulled = true
end)

UserInputService.InputEnded:Connect(function(input)
if input.KeyCode = Enum.KeyCode.MouseButton1 then
TriggerPulled = false
end)

while TriggerPulled == true do
– insert gun shooting mechanics here

6 Likes

How would I make the bullet fly towards where you shot at rather than it being a beam?
Great tutorial also!

2 Likes

How would I get it to update and fire from the shoot part if the character is moving?
Also, is there a way I can add entire teams to this RaycastParams function?

2 Likes

FilterDescendantsInstances is a table so I guess you can just iterate over the team and add their characters to it.

2 Likes

Bullet isnt working, dunno why.

4 Likes

Instead of cloning a gun from ServerStorage you can just use instance.new, but i agree cloning the Part from serverstorage is more useful and easier than using Instance.new

Edit: to answer for the post above me, im not sure but
Bullent_Clone.Size or Bullet_Clone.CFrame might be the cause of it

3 Likes

you should just clone the bullet and use bullet.Velocity = bullet.CFrame.look vector * speed

4 Likes

Because when you shoot backwards the ray just detects your arm or the gun model so it doesnt work but it should work if you shoot in front of you

3 Likes

This tutorial is made really Great! I like this tutorial! . Good job this it reminds me alot of prison life.!

2 Likes

How would I be able to make an automatic gun with this tutorial?

4 Likes

No. Raycasting on the server is completely fine, it isn’t “wasteful”.

Replicating to the client is completely useless and wasteful, you don’t want someone spam shooting the gun and fire loads of remotes to clients causing high network usage.

Also do know that you would be firing remote events to all the players in the game, which is even more wasteful in network usage, considering you will have more than 1 person with the gun.

EDIT:

Also, this tutorial is just for those to get started, not to account for details as incapaz stated.

4 Likes

A single ray has an insignificant impact on the server’s performance. You can usually shoot around 10-50 rays per frame on the server without causing any lag (don’t quote me on this as I’ve only tested this once).

2 Likes

Hey there, would there be any way to add a headshot multiplier to it?

2 Likes