Hey, guys. So I read a little about the Roblox Wiki article about raycast weapons, and I made the following script that makes the gun shoot. It is working fine:
local tool = script.Parent
Player = tool.Parent.Parent
local user
fire = script.Parent.Handle.Fire
Shooting = false
tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
if tool.Ammo.Value > 0 then
function shoot()
Shooting = true
local offset = {0.2,0.5,0,7}
local numnum = offset[math.random(1, #offset)]
local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p - tool.Hole.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(20)
end
local distance = (position - tool.Hole.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright yellow")
rayPart.Transparency = 0
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Hole.CFrame.p) * CFrame.new(0, 0, -distance/2)
tool.Ammo.Value = tool.Ammo.Value - 1
fire:Play()
game.Debris:AddItem(rayPart, 0.001)
wait(0.08)
end
repeat shoot() until not Shooting or tool.Ammo.Value <= 0
end
end)
mouse.Button1Up:connect(function()
Shooting = false
end)
end)
What I want to do now is make the gun have some accuracy! To do this, I want to multiply the ray's CFrame by a small random number so that it will be slightly offseted from the player's mouse. Notice how in lines 13 + 14, I made a variable called numnum that randomly chooses a number out of the table called offset. NOW HERE'S THE PROBLEM!!: Where do I multiply this number to?... I'd gladly appreciate it if someone could help me solve this problem. Thank You! Oh! BTW, Hole - mentioned in line 16 - is where the ray originates from (the barrel of the gun). It is a part.

2 comments
While setting the CFrame, make the second argument(Vector3 lookAt point) the Hole's CFrame multiplied by a randomized integer. It will essentially offset the end point, multiply on all axises for realistic offset.
Um... Can you give more detailed information? I'm still confused.