I've recently made a pistol using raycast, I can't figure out how to add spread.
Local script
01 | local BulletEvent = script.Parent:WaitForChild( "BulletEvent" ) |
02 | local debounce = false |
03 | script.Parent.Equipped:Connect( function (Mouse) |
04 | Mouse.Button 1 Down:Connect( function () |
05 | if debounce = = false then |
06 | debounce = true |
07 | script.Parent.BulletEvent:FireServer(script.Parent.BE.CFrame.Position,Mouse.Hit.Position) |
08 | wait( 0.5 ) |
09 | debounce = false |
10 | end |
11 | end ) |
12 | end ) |
Normal script
01 | script.Parent.BulletEvent.OnServerEvent:Connect( function (Player, FromP, ToP) |
02 | local RayCast = Ray.new(FromP,(ToP-FromP).unit* 100 ) |
03 | local Part,Position,Normal = game.Workspace:FindPartOnRay(RayCast,Player.Character, false , true ) |
04 | local Dist = (ToP-FromP).magnitude |
05 | if not Dist then Dist = 300 end |
06 | local Hole = game.ReplicatedStorage:FindFirstChild( "GlockBulletHole" ):Clone() |
07 | if Part ~ = nil then |
08 | Hole.Parent = Part.Parent |
09 | Hole.Position = ToP |
10 | Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+Normal) |
11 | local Weld = Instance.new( "Weld" ) |
12 | Weld.Part 0 = Part |
13 | Weld.Part 1 = Hole |
14 | Weld.C 0 = Part.CFrame:Inverse() |
15 | Weld.C 1 = Hole.CFrame:Inverse() |
Hello :) I've made my own spread system lately, I'll share it with you
1 | local spreadLevel = VALUE -- Your recoil level. It goes from 1 (bullets flying everywhere) to 100 (expert aim). You'll have to experiment a little bit |
2 |
3 | local spreadPoint = gun.PrimaryPart.CFrame * CFrame.new(-spreadLevel, 0 , 0 ) -- Creating a point in front of the gun. |
4 | local spreadX, spreadY = math.random(- 10 , 10 ), math.random(- 10 , 10 ) -- Getting random values of X spread and Y spread |
5 | local finalSpread = spreadPoint* CFrame.new(spreadX/ 10 , spreadY / 10 , 0 ) -- Calculating a final spread |
6 |
7 | -- After this is done, you have to launch a ray in direction of "finalSpread" position. |
If this code doesn't work, I recommend switching X and Z coordinate. So, last line would look like this:
1 | local finalSpread = spreadPoint* CFrame.new( 0 , spreadY / 10 , spreadX/ 10 ) |
That's how I made it. If you're having any trouble, let me know!