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)
