Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a gun attached to camera shoot?

Hi so recently I've been experimenting with the camera and so I scripted a gun to the camera. I honestly don't know how to make it shoot. Any help would be nice!

The video I watched: https://www.youtube.com/watch?v=T_uMjXoU8YQ

The framework code:

local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = player:GetMouse()
local rep = game:GetService("ReplicatedStorage")
local gun = rep.Guns.G36C
local run = game:GetService("RunService")

for i,v in pairs(gun:GetChildren()) do
	if v:IsA("BasePart") then
		if v ~= gun.PrimaryPart then
			local weld = Instance.new("Weld")
			weld.Part0 = gun.PrimaryPart
			weld.Part1 = v
			weld.C0 = gun.PrimaryPart.CFrame:inverse()
			weld.C1 = v.CFrame:inverse()
			weld.Name = v.Name
			weld.Parent = gun.PrimaryPart
		end
	end
end

gun.Parent = cam

run.RenderStepped:connect(function()
	gun:SetPrimaryPartCFrame(cam.CFrame*CFrame.new(0,-1.1,.2))
end)


I would like to make it shoot! THANKS!

1 comment

0

Any help works!

1 answer

The best place to make guns is here.

The tutorial linked will help you learn raycasts.

Here is some sample code:

-- Taken from the link above
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false

local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

game:GetService("Debris"):AddItem(beam, 0.1)

The code above, will create a beam towards the mouse position.

Here is another tutorial for guns, with animations!

Hope this helps!

4 comments

0

If you need any help with actully making it shoot, message me on roblox!

0

The youtube tutorial linked in the question will show how to make a FE gun.

0

Hi so this is my main account i sent you a message

0

nvm I can't :(

This discussion is preserved as it was. New answers, comments, and votes are closed.