Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I add aim innacuracy (spread) to my raycast pistol?

I've already asked this a few times and never got an answer I either understand how to implement or none at all, please help.

Local script

local BulletEvent = script.Parent:WaitForChild("BulletEvent")
local debounce = false
local v = 1
script.Parent.Equipped:Connect(function(Mouse)
 Mouse.Button1Down:Connect(function()
    if script.Parent:FindFirstChild("AbleToShoot").Value == true then
        local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local chr = plr.Character or plr.CharacterAdded:Wait()
local HP = chr:FindFirstChild("Humanoid").Health
        if HP > 0 then
    if debounce == false then
        debounce = true
    script.Parent.BulletEvent:FireServer(script.Parent.BE.CFrame.Position,Mouse.Hit.Position)
    wait(0.5)
    debounce = false
    end
        elseif v == 1 then
            v = 0
wait(2)
v = 1
    end
    end
end)
    end)

Normal script

script.Parent.BulletEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
 local RayCast = Ray.new(FromP,(ToP-FromP).unit*500)
 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") ~= nil then
  Part.Parent.Humanoid:TakeDamage(30)
  Hole:FindFirstChild("Front").Texture =  "rbxassetid://2078208894"
  Hole:FindFirstChild("Back").Texture =  "rbxassetid://2078208894"
else if Part.Parent.Parent:FindFirstChild("Humanoid") and Part ~= nil then
          Part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(30)
  Hole:FindFirstChild("Front").Texture =  "rbxassetid://2078208894"
  Hole:FindFirstChild("Back").Texture =  "rbxassetid://2078208894"
    end
    end
    else
            local gunshot = Instance.new("Sound", script.Parent)
gunshot.SoundId = "rbxassetid://356911785"
gunshot:Play()
script.Parent:FindFirstChild("Flare").MuzzleFlash.Enabled = true
script.Parent:FindFirstChild("Flare").Smoke.Enabled = true
wait(gunshot.TimeLength)
gunshot:Destroy()
end
if Part then
    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
script.Parent:FindFirstChild("Flare").Smoke.Enabled = true
wait(gunshot.TimeLength)
gunshot:Destroy()
end
end

2 comments

0

I answered you the last time, what is the problem? I'll help you with pleasure :)

0

i answered too!

1 answer

✓ Accepted answer

There's a vector3 spread with a random value between (-3,-3,-3) and (3,3,3) which is then added to the mouse position to make the shot inaccurate.

local BulletEvent = script.Parent:WaitForChild("BulletEvent")
local debounce = false
local v = 1
script.Parent.Equipped:Connect(function(Mouse)
 Mouse.Button1Down:Connect(function()
    if script.Parent:FindFirstChild("AbleToShoot").Value == true then
        local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local chr = plr.Character or plr.CharacterAdded:Wait()
local HP = chr:FindFirstChild("Humanoid").Health
        if HP > 0 then
    if debounce == false then
        debounce = true
	local spread = Vector3.new(math.random(-3,3),math.random(-3,3),math.random(-3,3))
    script.Parent.BulletEvent:FireServer(script.Parent.BE.CFrame.Position,Mouse.Hit.Position + spread)
    wait(0.5)
    debounce = false
    end
        elseif v == 1 then
            v = 0
wait(2)
v = 1
    end
    end
end)
    end)

1 comment

0

Doesn't work for bullet holes but I think I know how to fix that, thank you for helping

This discussion is preserved as it was. New answers, comments, and votes are closed.