New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

Raycast Gun Accuracy Help?

Asked by 5 years ago

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

    user = tool.Parent 
        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 =, (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

                local distance = (position - tool.Hole.CFrame.p).magnitude
                local rayPart ="Part", user)
                rayPart.Name          = "RayPart"
                rayPart.BrickColor    ="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          =, 0.2, distance)
                rayPart.CFrame        =, tool.Hole.CFrame.p) *, 0, -distance/2)
                tool.Ammo.Value = tool.Ammo.Value - 1
                game.Debris:AddItem(rayPart, 0.001)
            repeat shoot() until not Shooting or tool.Ammo.Value <= 0
        Shooting = false

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.

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. Goulstem 8078 — 5y
Um... Can you give more detailed information? I'm still confused. dpark19285 370 — 5y

1 answer

Log in to vote
Answered by 5 years ago

A few things:

You would add it on to the ray defining line, in this case, line 16.

Line 16: local ray =, (mouse.Hit.p + numnum - tool.Hole.CFrame.p).unit*300)

However, there are problems with this solution. First off all, make sure that you are considering negative values in your variance. At the moment the rays will only fire towards the top right. Secondly, this solution is flawed because your gun will have the same accuracy if you are on the other side of the map as it would if you were point blank. Believe me, I am having the same problem. If you can find a better way to fix this, please let me know, and if I find a better way, (I was kinda thinking about taking the X value of the ray into account) I will make sure to tell you. Happy coding!

Yes, thank you, that also works! But I also figured out another way to do it myself. Thank you so much for helping! dpark19285 370 — 5y

Answer this question