Raycast Going to Wrong Position Randomly

this bug i encountered some time ago came back even though i fixed it

script

local raycast = workspace:Raycast(Tool.FirePos.Position,(Mouse.Hit.Position-Tool.FirePos.Position)+Vector3.new(Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value))*1.2)
	local result
	local origin = Tool.FirePos.Position
	local target
	if raycast then
		target = raycast.Instance
		result = raycast.Position
	else
		result = Mouse.Hit.Position.Unit*Limit
	end
	script.Parent.RemoteEvent:FireServer("Shoot",target)

	local distance = (origin - result).Magnitude
	local p = Instance.new("Part")
	local s = Instance.new("SpecialMesh")
	s.MeshType = Enum.MeshType.Sphere
	s.Parent = p
	p.Anchored = true
	p.CanCollide = false
	p.Size = Vector3.new(0.1, 0.1, distance)
	p.CFrame = CFrame.lookAt(origin, result)*CFrame.new(0, 0, -distance/2)
	p.Transparency = .5
	p.Material = Enum.Material.SmoothPlastic
	p.BrickColor = BrickColor.Yellow()
	p.CanQuery = false
	p.CanCollide = false
	p.CanTouch = false
	
	p.Parent = workspace
	game.Debris:AddItem(p,.05)

keep in mind that it is the raycast itself messing up not the part as the output prints nothing i

i removed the spread fucntion and it worked , but what in the spread function messing it up?!?!?!

Well could it possibly be because of the first line

local raycast = workspace:Raycast(Tool.FirePos.Position,(Mouse.Hit.Position-Tool.FirePos.Position)+Vector3.new(Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value))*1.2) local result

it should instead be something like :


local raycast = workspace:Raycast(Tool.FirePos.Position, Mouse.Hit.Position - Tool.FirePos.Position)

if i do that, then there will be no spread… but with spread, it be MEsSSGING it up

try something like this , maybe?

local direction = (Mouse.Hit.Position - Tool.FirePos.Position).Unit
local spread = Vector3.new(Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value))
local raycast = workspace:Raycast(Tool.FirePos.Position, direction + spread * 1.2, RaycastParams.new())

ok i tried but now it always returns no raycast

local direction = (Mouse.Hit.Position - Tool.FirePos.Position).Unit
local spread = Vector3.new(Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value))
local raycast = workspace:Raycast(Tool.FirePos.Position, (direction + spread * 1.2).Unit)

s till it be a not working

  1. Raycast Direction: Ensure that the direction vector is a unit vector (a vector of length 1). If the length of the direction vector is greater than 1, the raycast might not hit anything because it’s going beyond the intended target.
  2. Raycast Length: The maximum length of a raycast is 15,000 studs. If the target is further away than this, the raycast will not hit it.
  3. Raycast Parameters: The RaycastParams object can be used to selectively include or exclude certain BaseParts, ignore the Water material for Terrain, or use a collision group. Make sure that the target is not being excluded by the RaycastParams.
  4. Target Properties: The target must be a BasePart or Terrain cell, and it must not be set to CanCollide = false. The Touched event will not fire for parts with CanCollide set to false.
  5. Workspace Settings: The Workspace.FilteringEnabled property must be set to true for raycasting to work.

the problem is that if i did something like this

	local spread = Vector3.new(Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value))
	local raycast = workspace:Raycast(Tool.FirePos.Position,(Mouse.Hit.Position-Tool.FirePos.Position)*1.2)

no matter what if i add it after the parthesis

image

it will bug like it did in the video

This could be due to the script or the game just lagging. You could add a debounce to your script to reduce the lag with something like this local canFire = true

it is something to do with the spread idk ill try to find a fix for it

well if this script runs extremely fast without a debounce it could lead to the script being slow and messing up the spread

well there is a firerate that is .1 secondes

fizexed by doing

	local spread = Vector3.new(new:NextNumber(-Values.Spread.Value,Values.Spread.Value),new:NextNumber(-Values.Spread.Value,Values.Spread.Value),new:NextNumber(-Values.Spread.Value,Values.Spread.Value))
	local raycast = workspace:Raycast(Tool.FirePos.Position,(Mouse.Hit.Position-spread-Tool.FirePos.Position)*1.2)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.