Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My script isnt working? My raycast shoots when I click at ground but not sky!

Okay I made a raycast script for a gun. Basically if the ray doesnt hit anything, it creates another ray so on so on until hit ~= nil. Everything works when I shoot at a part! But when I shoot in the sky, it doesnt work! This is so infurating!--don't mind spelling.. Please tell me where I went wrong, thank you! ~~~~~~~~~~~~~~~~~ --[[SETUP-----------------------------------------------]] local tool = script.Parent local plr = game:GetService("Players").LocalPlayer

--[[Config----------------------------------------------]]

--[[Main------------------------------------------------]] tool.Equipped:connect(function(mouse) print("Tool Has Been Equipped") mouse.Button1Down:connect(function()

	local rayLength = {}
	local drop = {}
	
	rayLength[1] = 30
	drop[1] = .1
	local mouseHitP = mouse.hit.p
	local ray = Ray.new(tool.Handle.CFrame.p,(mouseHitP - tool.Handle.CFrame.p).unit * 1)
	local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
	
	local visRay = Instance.new("Part", workspace)
	visRay.BrickColor = BrickColor.new("Brick yellow")
	visRay.Locked = true
	visRay.Anchored = true
	visRay.CanCollide = false
	visRay.Material = ("Neon")
	visRay.Transparency = (.5)
	visRay.FormFactor = ("Custom")
	local distance = (tool.Handle.CFrame.p - position).magnitude
	visRay.Size = Vector3.new(.25,.25,distance)
	visRay.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0,0,-distance / 2)
	local oldPosition = position
	visRay:Destroy()
	print (hit)
if hit == nil then
repeat
	local ray = Ray.new(oldPosition,(mouseHitP - oldPosition).unit * rayLength[1] + Vector3.new(0,-drop[1],0))	
	local hit, position = workspace:FindPartOnRay(ray,plr, false ,true)
	local visRay = Instance.new("Part", workspace)
	visRay.BrickColor = BrickColor.new("Brick yellow")
	visRay.Locked = true
	visRay.Anchored = true
	visRay.CanCollide = false
	visRay.Material = ("Neon")
	visRay.Transparency = (.5)
	visRay.FormFactor = ("Custom")
	local distance = (mouseHitP - oldPosition).magnitude
	visRay.Size = Vector3.new(.25,.25,distance)
	visRay.CFrame = CFrame.new(oldPosition, position) * CFrame.new(0,0,-distance / 2)
	oldPosition = position
	drop[1] = (drop[1] - .1)
	wait(.05)
	visRay:Destroy()
until
hit ~= nil
end
end)

end)

 

1 comment

1 answer

I believe the reason is that the Sky is not a Part, and since its an unlimited space/void, the script cannot detect a Part, UNLESS you create invisible blocks in each side of your game, do the same for top.

Just simply change the Transparency of each Block to 1, and make sure its Anchored.

That is all i knew, hope it helps!

1 comment

0

I would do that, but that would only find a way around the problem, not fix it. I made an old one that used to work, and for some reason this new one isn’t working... It should work either way because it has nothing to do with the part, which I don’t understand. I put if hit is nil, add another ray... it’s still being nil at the end of the Ray, so it should continue to add another ray, however it

This discussion is preserved as it was. New answers, comments, and votes are closed.