How would I alter my script to raycast into the sky?

So my raycast makes a raycast using the mouse.Hit.Position, and a part visualizer. But whenever I try to raycast in the sky it doesn’t make a raycast. I’ve figured out that this is due to the MousePos being to far. I’ve tried UnitRay but it wasn’t a vector3 so I didn’t see how to incorporate it. I’m also pretty sure the problem is the raycast itself not the part.

So if someone could help me figure out a method it would be greatly appreicated.

---Variables---

local RepStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

local MediToolActivated = RepStorage:WaitForChild("MediToolActivated")
local RayIgnoreFolder = game.Workspace:WaitForChild("RayIgnoreList")

---Coding---

--Making the MediGun work

MediToolActivated.OnServerEvent:Connect(function(player, MousePos, Mouse, MediTool)

	local MediTip = MediTool.Tip
	local Character = player.Character or player.CharacterAdded:Wait()
	local RootPart = Character:WaitForChild("HumanoidRootPart")
	local Humanoid = Character:WaitForChild("Humanoid")
	
	local RayParams = RaycastParams.new()
	RayParams.FilterType = Enum.RaycastFilterType.Blacklist
	RayParams.FilterDescendantsInstances = {player, RayIgnoreFolder, Mouse, Character}
	
	local RayDirection = (MousePos - MediTip.CFrame.p).Unit
	
	local RayResults = workspace:Raycast(MediTip.CFrame.p, RayDirection * 50, RayParams)
	if RayResults then
		print(RayResults.Position)
		local RayEndPos = RayResults.Position
		local StartPoint = MediTip.CFrame.p
		local MidPoint = StartPoint + (RayEndPos - StartPoint)/2
		
		local RaySizeXY = .3
		
		local RayPart = Instance.new("Part")
		RayPart.Name = "RayPart"
		RayPart.Anchored = true
		RayPart.CanCollide = false
		RayPart.Material = Enum.Material.Neon
		RayPart.Color = Color3.fromRGB(255, 0, 0)
		RayPart.CFrame = CFrame.new(MidPoint, StartPoint)
		RayPart.Size = Vector3.new(RaySizeXY,RaySizeXY,(RayEndPos - StartPoint).magnitude)
		RayPart.Parent = RayIgnoreFolder
		
		local RayHum = RayResults.Instance.Parent:FindFirstChild("Humanoid")
		
		if RayHum then
			if RayHum.Health < 100 then
				RayHum.Health = RayHum.Health + 1
			end
		end
		
		game.Debris:AddItem(RayPart,.1)	
	end	
end)
2 Likes

It’s actually still raycasting, but when the ray doesnt hit anything after the specified distance RaycastResults return nil.
If you still want to see a ray even when it doesn’t hit anything you should put an else and do the same method you used to create the ray, except RayEndPos is now StartPoint + RayDirection*50

if RaycastResults then
local Endpoint = RaycastResults.Position
--Create a ray
else
local Endpoint = StartPoint + Direction*50
--Create a ray
end
1 Like

Alright, so I tried your method and I changed it to this. I also had tried just changing the variables of RayEndPos but It never returns nil for some reason? It will never print yes no matter what.

	if RayResults then
		
		local StartPoint = MediTip.CFrame.p
		if RayResults then
			local RayEndPos = RayResults.Position
			local MidPoint = StartPoint + (RayEndPos - StartPoint)/2	
			local RaySizeXY = .3	
			RayPart = Instance.new("Part")
			RayPart.Name = "RayPart"
			RayPart.Anchored = true
			RayPart.CanCollide = false
			RayPart.Material = Enum.Material.Neon
			RayPart.Color = Color3.fromRGB(255, 0, 0)
			RayPart.CFrame = CFrame.new(MidPoint, StartPoint)
			RayPart.Size = Vector3.new(RaySizeXY,RaySizeXY,(RayEndPos - StartPoint).magnitude)
			RayPart.Parent = RayIgnoreFolder
		else
			print("Yes")
			local RayEndPos = StartPoint + RayDirection * 50
			local MidPoint = StartPoint + (RayEndPos - StartPoint)/2	
			local RaySizeXY = .3	
			RayPart = Instance.new("Part")
			RayPart.Name = "RayPart"
			RayPart.Anchored = true
			RayPart.CanCollide = false
			RayPart.Material = Enum.Material.Neon
			RayPart.Color = Color3.fromRGB(255, 0, 0)
			RayPart.CFrame = CFrame.new(MidPoint, StartPoint)
			RayPart.Size = Vector3.new(RaySizeXY,RaySizeXY,(RayEndPos - StartPoint).magnitude)
			RayPart.Parent = RayIgnoreFolder
		end
	```

What i meant was that sometimes RayResults returns nil, because it didnt hit any part, so you have to get the endpos some other way. If you aim at a part it wont print “Yes” but if you aim at the sky it will

1 Like

Yeah, I understood what you meant by when nothing is found it returns nil and I have to create the endpos in another way. But for some reason it just doesn’t return nil heres an example. https://gyazo.com/34d17edc99663921cae9de775f234357

1 Like

I found the issue.

You put “if RayResults then” twice, get rid of the second one and it should work

1 Like

Indeed, that was the issue I put it there because of the fact that I also wanted to heal someone if found, inside of the same RayResults i’ll just have to find a new method. Thank you so much for the help though this took me too long.

3 Likes

Glad i was able to help. Good luck on your projects!

2 Likes