Raycast not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I’m trying to make a function that will give me a random floor, using raycasts.

  2. What is the issue?
    The function returns as nil.

  3. What solutions have you tried so far?
    I added the “repeat untill” and i checked the forums

We are assuming that argument 1 and 2 are both nil.

function module.GetRandomFloor(mode, range)
	-- define variables
	local vector = nil
	-- fixed stats(?)
	local direction = Vector3.new(0, -90, 0)
	-- autofil
	if range == nil then range = 64 end
	if mode == nil then mode = 0 end
	-- modes
	local ray = nil
	repeat
		if mode == 0 then 
			-- true random
			vector = Vector3.new(math.random(-range,range),math.random(-range,range),math.random(-range,range))
		end
		local ray = workspace:Raycast(vector,direction)
		print("Casting ray")
	until ray ~= nil
	print("Ray sucess")
	return {ray.Position, ray.Distance}
	-- cast ray
	-- return
end

you are localizing the ray within the repeat loop, this means the other ray variable you made will never be assigned to anything. If you haven’t already, take a look at how scopes work

1 Like

Ah. Right. I did that because i hadn’t previously defined “ray”. Thanks!

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