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

Why wont WorldRoot:Raycast work sometimes?

I made a tool that casts a ray to the position of Mouse.Hit.p, and a beam would appear between the tool's handle and RaycastResult.Position. However sometimes the ray doesn't seem to hit anything at all so the beam doesn't appear. I know for sure that this is is a problem with raycasting and not the beam since WorldRoot:Raycast wont return anything.

-- Only important parts:
local function fireLaser(player, MousePosition)
	local origin = LaserPart.Position
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = GetParamsList:Invoke(player)
	params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local rayCast = game.Workspace:Raycast(origin, (MousePosition - origin).Unit, params)
	
	if not rayCast then
		return
	end
	
	local aPart = AttachmentPart:Clone()
	aPart.Script.Disabled = false
	aPart.Position = rayCast.Position
	aPart.Parent = game.Workspace
	Beam.Attachment1 = aPart.Attachment
end

I also made a turret that tracks (p1.CFrame = CFrame.new(p1.Pos, p2.Pos)) the first player that it sees, and the same problem occurs.

-- Only important parts:
local params = RaycastParams.new()
local FDI = script.Parent:GetChildren()
for k, v in pairs(TurretPart:GetChildren()) do
	table.insert(FDI, v)
end
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = FDI

while true do
	-- ...
	wait(1)
	hit = game.Workspace:Raycast(TurretPart.Position, (hrp.Position - 	TurretPart.Position).Unit * BulletSpeed, params)
end

Also, there's a Ray object separate from WorldRoot:Raycast, but I don't know how to use it.

1 answer

if a ray doesn't hit anything, WorldRoot:Raycast will return nil

the old raycasting api did return the position where the ray would have ended, but the old api has been deprecated for a while now, so you shouldn't use it

you can find the position where the ray would have ended by just doing origin + direction

local dir = (MousePosition - origin).Unit
local rayCast = game.Workspace:Raycast(origin, dir, params)

local aPart = AttachmentPart:Clone()
aPart.Script.Disabled = false
aPart.Position = rayCast and rayCast.Position or origin + dir -- this almost works like an if statement, it's like doing if condition then a, else b
-- they're not exactly the same though
aPart.Parent = game.Workspace
Beam.Attachment1 = aPart.Attachment

2 comments

0

This works, but defeats the purpose of raycasting. If the player were to go in third person, they would be able to cast the beam through any obstructions. Same for the turret; it can see through obstructions.

0

why would they be able to cast through obstructions?? the origin would (presumably) be at the barrel of their gun, and if there's something between the player's mouse and the gun, then Raycast will have to return something

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