I've recently made a pistol using raycast, I can't figure out how to add spread.
Local script
local BulletEvent = script.Parent:WaitForChild("BulletEvent") local debounce = false script.Parent.Equipped:Connect(function(Mouse) Mouse.Button1Down:Connect(function() if debounce == false then debounce = true script.Parent.BulletEvent:FireServer(script.Parent.BE.CFrame.Position,Mouse.Hit.Position) wait(0.5) debounce = false end end) end)
Normal script
script.Parent.BulletEvent.OnServerEvent:Connect(function(Player, FromP, ToP) local RayCast = Ray.new(FromP,(ToP-FromP).unit*100) local Part,Position,Normal = game.Workspace:FindPartOnRay(RayCast,Player.Character, false,true) local Dist = (ToP-FromP).magnitude if not Dist then Dist = 300 end local Hole = game.ReplicatedStorage:FindFirstChild("GlockBulletHole"):Clone() if Part ~= nil then Hole.Parent = Part.Parent Hole.Position = ToP Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+Normal) local Weld = Instance.new("Weld") Weld.Part0 = Part Weld.Part1 = Hole Weld.C0 = Part.CFrame:Inverse() Weld.C1 = Hole.CFrame:Inverse() Weld.Parent = Hole if Part and Part.Parent:FindFirstChild("Humanoid") then Part.Parent.Humanoid:TakeDamage(30) Hole:FindFirstChild("Front").Texture = "rbxassetid://2078208894" Hole:FindFirstChild("Back").Texture = "rbxassetid://2078208894" end if Part.Parent ~= nil then if Part:FindFirstChild("IsGunHole") ~= nil then Hole:Destroy() else game.Debris:AddItem(Hole,10) end local gunshot = Instance.new("Sound", script.Parent) gunshot.SoundId = "rbxassetid://356911785" gunshot:Play() script.Parent:FindFirstChild("Flare").MuzzleFlash.Enabled = true wait(gunshot.TimeLength) gunshot:Destroy() end end end)
Hello :) I've made my own spread system lately, I'll share it with you
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 local spreadPoint= gun.PrimaryPart.CFrame * CFrame.new(-spreadLevel, 0, 0) -- Creating a point in front of the gun. local spreadX, spreadY = math.random(-10, 10), math.random(-10, 10) -- Getting random values of X spread and Y spread local finalSpread= spreadPoint* CFrame.new(spreadX/10, spreadY /10, 0) -- Calculating a final spread -- 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:
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!