Does raycasting yield?

For some reason, the ray is not being registered yet at --Calc block position. The ray is set to nil until a few milliseconds later. The function util.raycast casts a ray from the mouse with rayparams as an input.

function placer1:start()
	local ray_params = RaycastParams.new()
	ray_params.FilterType = Enum.RaycastFilterType.Include
	ray_params.FilterDescendantsInstances = workspace.flightworks.plots:GetChildren()

	self.update = storage_util.events:grab("heartbeat"):Connect(function()
		local cframe = self.plot.part.CFrame
		local ray = mouse_util.raycast(ray_params)
		task.wait()
		
		if ray then
			--Calc block position
		end	
	end)
end

Shouldn’t the code yield for the creation of the ray? I am still a little confused on why the code is continuing before the ray exists.

The variable is already set before your wait begins, so its value isn’t changed. Add the wait before you set the variable.

1 Like

Making a ray doesn’t yield the code, the ray is handled synchronously, so the ray should not be nil, unless there is some weird stuff going on with the mouse_util.raycast function

this did fix the code, but i am a little confused on how it does. is it okay if you could explain a little bit?

This is pretty much what’s happening.

Code with comments:

local function getNumber()
	return 3
end

local myCoolVariable = getNumber() -- The function is called only ONCE, and this variable is now set to what the function returned (which is 3)

task.wait(1) -- No matter how long you wait, the value of the variable above won't change on it's own

print(myCoolVariable)

I’m guessing you think that the function is called over and over again to update the variable, but the function is only called once when you’re assigning it.

ah, i thought you were trying to say something much more complicated. i was wondering why moving the wait before the variable fixed things. in my code, having no wait made the ray return nil, and so did having the wait after the variable was defined.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.