Raycast is not hitting, unless up close

So, I have a script that controls a gun
Everything is fine, but the bullets wont register a hit even though they should have hit
They only hit when you put the barrel inside of the target

  shoot.OnServerEvent:connect(function(Player, FromP, ToP, ammoCount)
if ammoCount.Value ~= 0 and reloading == false then
	ammoCount.Value = ammoCount.Value - 1
	local character = Player.Character
	local hum = character.Humanoid

	local shootTrack = hum:LoadAnimation(script.GunShoot)
	local idleTrack = hum:LoadAnimation(script.GunIdle)
	
	local direction = CFrame.new(FromP, ToP)*CFrame.Angles(math.rad(math.random(-spread, spread)), math.rad(math.random(-spread, spread)), 0)
	local rayCast = Ray.new( FromP,direction.LookVector, (ToP-FromP).unit* 100)
	local Part, Position = game.Workspace:FindPartOnRay(rayCast, Player.Character, false, true)
	if Part then
		local huma = Part.Parent:FindFirstChild("Humanoid")
		if huma and Part.Name == "Head" then
			huma:TakeDamage(Damage * headShotMultiplier)
		elseif huma then
			huma:TakeDamage(Damage)
		end
	end
	--//Sound
	local pew = Instance.new("Sound")
	pew.SoundId = "rbxassetid://1136243671"
	pew.Parent = Player.Character:WaitForChild("P90").Barrel
	pew.Volume = 2
	pew:Play()
	--//Laser
	local laser = Instance.new("Part")
	laser.Parent = game.Workspace
	laser.CanCollide = false
	laser.Color = Color3.new(0.968627, 1, 0.298039)
	laser.Transparency = 0.5
	laser.Anchored = true
	--//Shooting
	shootTrack:Play()
	local dist = (ToP-FromP).magnitude
	laser.CFrame = CFrame.new(FromP, Position)*CFrame.new(0,0,-dist/2)
	laser.Size = Vector3.new(0.1,0.1,dist)
	game.Debris:AddItem(laser,0.05)
	shootTrack.Stopped:Wait()
	idleTrack:Play()
	wait(2)
	pew:Destroy()
end

end)

I tried changing the “unit * 100” to different values but still nothing

Whats probally happening is the raycast is hitting the thing its raycasting from I would recommend using the blacklist feature to make it go straight through the gun

Sadly Roblox created a new raycasting thing and replaced the old documentation so I have no idea where to find out how to do that

1 Like