Raycast Visualize Part goes in sky

I have a gun system and it works (exepct for the raycasting on humanoids half the time but I’ll fix that later)
But the problem is that when it shoots in the sky, it has no limit to how far it can shoot
Raycast :

	local Cast = workspace:Raycast(script.Parent.FirePos.Position,MousePos - script.Parent.FirePos.Position)

Part Visualize

if Config.VisibleRaycastBullet.Value == true then
		local distance = (origin - result).Magnitude
		local p = Instance.new("Part")
		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.BrickColor = BrickColor.new("New Yeller")
		p.Parent = workspace
		p.Transparency = .5
		p.Material = Enum.Material.SmoothPlastic
		p.CanQuery = false
		p.CanTouch = false
		local t = game.TweenService:Create(p,TweenInfo.new(Config.VisibleRaycastBullet.Time.Value),{Transparency = 1})
		game.Debris:AddItem(p,Config.VisibleRaycastBullet.Time.Value)
	end

image

video


BRO LOWER YOUR VOLUME ON VIDEO WHEN I WAS TESTING ITS NOT THAT LOUD

( I have no idea the message at top in video some kinda virus plugin that idk what it is but I don’t really care )

2 Likes

Ineed help for the problem of the raycast

2 Likes

It looks like you have a variable for the distance, so couldn’t you just use an if statement to check if the number is less than (<) a certain value?

1 Like

but how Would I do that I don’t understand it I’m not that good with ray casting

this is how i did it before
its important to note that this uses the deprecated raycasting though

(you dont need ignore list if you dont want)

local mouseRay = mouse.UnitRay
local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.Unit * MaxPlaceDistance)
target, pos, norm = workspace:FindPartOnRayWithIgnoreList(newRay, ignoreList)
1 Like

I tried making my code this but it still vanish into the sky

		local Cast = workspace:Raycast(script.Parent.FirePos.Position,MousePos - script.Parent.FirePos.Position * 2000)

It still works normally on part though

1 Like

what is MousePos? can you tell me what the variable to the mouse is? in your script, or just give me more of the script?

1 Like

Ohhh Ok so inside my gun are two scripts and remoteevent : “Client” and “Server” script, and RemoteEvent “Action”

script.Parent.Action.OnServerEvent:Connect(function(Player,Action,MousePos)
	
	if Action == "Shoot" then
		local Cast = workspace:Raycast(script.Parent.FirePos.Position,(MousePos - script.Parent.FirePos.Position) * 2000)
		local origin = script.Parent.FirePos.Position
		local result = MousePos

		local SFX = Config.SFX.Fire:Clone()
		SFX.Parent = script.Parent.Handle
		SFX:Play()
		game.Debris:AddItem(SFX,SFX.TimeLength)

		if Config.VisibleRaycastBullet.Value == true then
			local distance = (origin - result).Magnitude
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.Size = Vector3.new(0.05, 0.05, distance)
			p.BrickColor = BrickColor.new("New Yeller")
			p.Parent = workspace
			p.Transparency = .5
			p.Material = Enum.Material.Neon
			p.CanQuery = false
			p.CanTouch = false
			p.CFrame = CFrame.lookAt(origin, result)*CFrame.new(0, 0, -distance/2)
			local t = game.TweenService:Create(p,TweenInfo.new(Config.VisibleRaycastBullet.Time.Value),{Transparency = 1})
			game.Debris:AddItem(p,Config.VisibleRaycastBullet.Time.Value)
		end

Part of server script, the raycast and the raycast part

Shoot on client fire to Server :

image

image

In the parameters, pass over mouse.UnitRay. no need to remove any other parameters, just add one

local Cast = workspace:Raycast(script.Parent.FirePos.Position,MouseRay.Direction.Unit  * 2000)

make sure that you name the mouse.UnitRay thing you pass over as MouseRay

I replace lines on Server:

Fire line:

script.Parent.Action.OnServerEvent:Connect(function(Player,Action,MousePos,MouseRay)

RayCast line:

local Cast = workspace:Raycast(script.Parent.FirePos.Position,MouseRay.Direction.Unit  * 2000)

I replace lines on the Client:

Fire Action line:

	script.Parent.Action:FireServer("Shoot",Mouse.Hit.Position,Mouse.UnitRay)

But it still goes to sky

This has been solved here: