Trying to learn to script guns, so I decided I would try and make one using part of the wikis script and some of my coding.
local tool = script.Parent
local user
user = tool.Parent
tool.Equiped:connect(function(mouse)
missile = Instance.new("Part")
missile.Name = "Bullet"
missile.Shape = "Block"
missile.Anchored = false
missile.CanCollide = true
missile.CFrame = handle.CFrame
missile.Size = Vector3.new(1, 1.2, 7)
missile.BrickColor = BrickColor.new("Really black") --when the left mouse button is clicked
mouse.Button1Down:connect(function()
--make and do a hit test along the ray
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
--do damage to any humanoids hit
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end)
end)

1 comment
You never use the bullet you created. 'user' should not be defined until the tool is equipped, because otherwise it won't equal the character. It is also much cleaner to create the bullet in a separate function.