How can I make my gun have lower accuracy?

I’m making a gun.

  1. What do you want to achieve?

I want to make it so that the gun has low accuracy, Basically, I want the Raycast’s end not be exactly where the player’s mouse was.

What is the issue?

I have no Idea how to do this, I suppose I’d have to add a number from -3 to 3 to the X, Y and Z… I tried doing this, but If you shoot a wall that’s very close to you, the RayCast end is way too offset. So I suppose I’d have to know how long the raycast is and depending on that add or substract to the end position.

What solutions have you tried so far?

As I mentioned, I tried just adding from -3 to 3 to the X, Y and Z.

I am not very advanced at scripting… So it was pretty hard for me to make that script, and I’m scared to modify it and break it.

image

"MouseDetect2


local Equipped = false
local plr = game.Players.LocalPlayer

repeat wait() until game.Players.LocalPlayer.Character
local char1 = plr.Character
local Anim = char1.Humanoid:LoadAnimation(script.Parent.Idle)
local Anim2 = char1.Humanoid:LoadAnimation(script.Parent.Anim)

local RightArm = char1.Torso["Right Shoulder"].C0
local LeftArm = char1.Torso["Left Shoulder"].C0
local Neck = char1.Torso["Neck"].C0

script.Parent.GivePlayer:FireServer(plr)

-------------------

script.Parent.Equipped:Connect(function(mouse)
	
	mouse.Button1Down:Connect(function()
		
		local Target = mouse.Target
		--local Gun = script.Parent:FindFirstChild("Gun")
		
		if Target and Target:IsA("Part") and Target.CanCollide == false or Target and Target:IsA("MeshPart") and Target.CanCollide == false then

			mouse.TargetFilter = Target

		end
		
		script.Parent.Event:FireServer(script.Parent.GunModel.Potato.Position, mouse.Hit.p)
		
	end)
	
end)

script.Parent.Equipped:Connect(function()

	Anim:Play()

end)

script.Parent.Unequipped:Connect(function()

	Anim:Stop()

end)

script.Parent.PunchAnim.OnClientEvent:Connect(function()

	Anim2:Play()

end)

“Shoot”

local Cooldown = false
local NonCollideDescendants = {}

local plr = nil

script.Parent.GivePlayer.OnServerEvent:Connect(function(player)

	plr = player

end)


local function MakeBullet(Start, End, Dist) 
	
	local Dist2 = (Start - End).magnitude
	
	local MidPoint = Start + End / 2
	local Lazer = Instance.new("Part")
	Lazer.Parent = game.Workspace
	Lazer.Material = Enum.Material.Neon
	Lazer.BrickColor = BrickColor.new("Cool yellow")
	Lazer.Parent = game.Workspace
	Lazer.Anchored = true
	Lazer.CanCollide = false
	Lazer.Size = Vector3.new(.1, .1, Dist2)
	Lazer.CFrame = CFrame.new(Start, End) * CFrame.new(0, 0, - Dist2 / 2)
	Lazer.Name = "Lazer"
	
	wait(.1)
	Lazer:Destroy()
	
end

script.Parent.Event.OnServerEvent:Connect(function(player, FromP, ToP)
	
	local PlayerStuff = nil
	
	for i, Descendantss in pairs(player.Character:GetDescendants()) do

		if Descendantss:IsA("Part") or Descendantss:IsA("MeshPart") then

				PlayerStuff = Descendantss

		end

	end
	
	if Cooldown == false then
		
		script.Parent.PunchAnim:FireClient(plr)
		wait(.1)
		
		Cooldown = true
		local Pos = ToP
		local CharacterParts = player.Character:GetDescendants()
		
		--local RayCast = Ray.new(script.Parent.Handle.Position, (ToP - FromP).unit * 100)
		local DirectionRay = (ToP - FromP).Unit * 100
		
		local NonCollidePartsToIgnore = NonCollideDescendants
		
		local PlayerStuff = nil
		
		local Params = RaycastParams.new()
		Params.FilterType = Enum.RaycastFilterType.Blacklist
		
		for i, Descendants in pairs(game.Workspace:GetDescendants()) do

			if Descendants:IsA("Part") or Descendants:IsA("MeshPart") then

				if  Descendants.CanCollide == false then

					table.insert(NonCollideDescendants, Descendants)

				end

			end

		end
		
		Params.FilterDescendantsInstances = {
			
			player.Character,
			NonCollideDescendants	

		}
		
		local RayCastResult = workspace:Raycast(FromP, DirectionRay, Params)
		local Dist = (ToP - FromP).magnitude
		
		local Destination = nil
		
		if RayCastResult ~= nil then
			
			Destination = RayCastResult.Position
			
		else
			
			Destination = ToP
			
		end
		
		local Source = FromP
		local Direction = Vector3.new(Destination.X - Source.X, Destination.Y - Source.Y, Destination.Z - Source.Z)

		local Cast = workspace:Raycast(Source, Direction)
		
		local Sound = script.Parent.Sound
		Sound:Play()
		
		if RayCastResult then
			
			MakeBullet(Source, Destination, Direction)
			print("Fired1")
			
		else
			
			MakeBullet(Source, ToP, Direction)
			print("Fired2")
			
		end
		
		if RayCastResult then
			
			local Hit = RayCastResult.Instance
			
			if Hit and Hit.Parent:FindFirstChildOfClass("Humanoid") and not Hit:IsDescendantOf(player.Character) then
				
				local Hum = Hit.Parent:FindFirstChildOfClass("Humanoid")
				Hum:TakeDamage(50)
				
			end
			
			
		end
		
		script.Parent.Sound:Play()
		
		wait(.5)
		Cooldown = false
			
	end
	
end)

script.Parent.Equipped:Connect(function()

	plr.Character.ClientHeadMovement.MoveRArm.Value = true
	plr.Character.ClientHeadMovement.MoveLArm.Value = true
	--plr.Character.ClientHeadMovement.MoveNeck.Value = true

end)

script.Parent.Unequipped:Connect(function()

	plr.Character.ClientHeadMovement.MoveRArm.Value = false
	plr.Character.ClientHeadMovement.MoveLArm.Value = false
	--plr.Character.ClientHeadMovement.MoveNeck.Value = false

end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You could maybe use the magnitude of the original mouse position and the position of the gun or the head. Multiply the magnitude by the math.random(-3.0,3.0) and divide by 60 or 100. :man_shrugging:

1 Like