How do I add spread to my raycast pistol?
I've asked this before and got an answer, but I didn't fully understand it.
Code instead of an explanation would be greatly appreciated.
Local script:
01 | local BulletEvent = script.Parent:WaitForChild( "BulletEvent" ) |
04 | script.Parent.Equipped:Connect( function (Mouse) |
05 | Mouse.Button 1 Down:Connect( function () |
06 | if script.Parent:FindFirstChild( "AbleToShoot" ).Value = = true then |
07 | local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait() |
08 | local chr = plr.Character or plr.CharacterAdded:Wait() |
09 | local HP = chr:FindFirstChild( "Humanoid" ).Health |
11 | if debounce = = false then |
13 | script.Parent.BulletEvent:FireServer(script.Parent.BE.CFrame.Position,Mouse.Hit.Position) |
Normal script
01 | script.Parent.BulletEvent.OnServerEvent:Connect( function (Player, FromP, ToP) |
02 | local RayCast = Ray.new(FromP,(ToP-FromP).unit* 500 ) |
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() |
08 | Hole.Parent = Part.Parent |
10 | Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+Normal) |
11 | local Weld = Instance.new( "Weld" ) |
14 | Weld.C 0 = Part.CFrame:Inverse() |
15 | Weld.C 1 = Hole.CFrame:Inverse() |
17 | if Part and Part.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
18 | Part.Parent.Humanoid:TakeDamage( 30 ) |
21 | else if Part.Parent.Parent:FindFirstChild( "Humanoid" ) and Part ~ = nil then |
22 | Part.Parent.Parent:FindFirstChild( "Humanoid" ):TakeDamage( 30 ) |
28 | local gunshot = Instance.new( "Sound" , script.Parent) |
31 | script.Parent:FindFirstChild( "Flare" ).MuzzleFlash.Enabled = true |
32 | script.Parent:FindFirstChild( "Flare" ).Smoke.Enabled = true |
33 | wait(gunshot.TimeLength) |
37 | if Part.Parent ~ = nil then |
38 | if Part:FindFirstChild( "IsGunHole" ) ~ = nil then |
41 | game.Debris:AddItem(Hole, 10 ) |
43 | local gunshot = Instance.new( "Sound" , script.Parent) |
46 | script.Parent:FindFirstChild( "Flare" ).MuzzleFlash.Enabled = true |
47 | script.Parent:FindFirstChild( "Flare" ).Smoke.Enabled = true |
48 | wait(gunshot.TimeLength) |