Hello. I’m making a 2d game with guns and I can not figure out how to make a gun spread like this:
This is the script right now:
local remote = script.Parent:WaitForChild("Raycast")
remote.OnServerEvent:Connect(function(player, position)
for i=1,10 do
local hrp = player.Character.HumanoidRootPart
local origin = hrp.Position
local direction = (position - origin).Unit*300
local result = workspace:Raycast(origin, direction)
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = Instance.new("Part")
bullet_clone.Name = "Ray"
bullet_clone.Anchored = true
bullet_clone.CanCollide = false
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = workspace
game.Debris:AddItem(bullet_clone,.25)
if result then
local part = result.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end
end
end)
If you have any ideas on how to make this , please reply to my post.