Help with Returning two Values

I want to know how I can get two values out of a Return and assign them to a Variable in another function.

I don’t understand how to assign the second returned value to a variable. I’ve also checked if the value actually is being changed in the higher function and it is. (Will be marked)

I tried looking on the DevForum but couldn’t find a clear solution.

Here is the code:

function raycast(v,p)
	local rayOrgin = char.HumanoidRootPart.Position
	local rayDirection = char.HumanoidRootPart.CFrame.LookVector * Vector3.new(0,0,11)
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {char}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local result = workspace:Raycast(rayOrgin,rayDirection,params)
	
	if result then
		print(result.Instance)
		local hit = result.Instance
		local hitp = result.Position
		print("This is what the hit position is: " .. tostring(hitp)) -- this returns the needed Vector3 value
		if hit.CanCollide == false then return end
		return v == hit and p == hitp
	end
end

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		local hitpart, partpos = raycast()
		print(hitpart)
		print("This is what the return thinks it is: " .. tostring(partpos)) -- this returns nil
		if hitpart == nil then
			char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-distance)
			hitpart = nil
			for i = 1,10 do
				
			end
		elseif partpos ~= nil then
			char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(partpos.X,partpos.Y,partpos.Z)
		end
	end
end)

To return 2 values, you must put a comma between them

return v == hit, p == hitp
1 Like

Never mind, the problem is still there, it doesn’t look like the comma was an issue in the first place.

For some reason the second value is being returned as Boolean: False instead of a Vector3 even though the function says the value is a Vector3.

 This is what the hit position is: 6.697047233581543, 2.9999992847442627, -28  -  Client - Dash:23
 false  -  Client - Dash:32
 This is what the return thinks it is: false  -  Client - Dash:33

The only thing i can think of here is that the second value is still being assigned as the first returned value.

This is because, when you use the and operator, it returns a boolean (as a result of wether both values given are true). If you want to return the hit instance and position, do as @SOTR654 suggested with a comma.

Example:

local function Raycast()
    local hitObject, hitPos = 3, 5
    return hitObject, hitPos
end

local object, pos = Raycast()
print(object, pos) -- 3, 5
print(object) -- 3
print(pos) -- 5

I already did and the script is still returning a boolean

Could you send me your code so I can take a look?

function raycast(v,p)
	local rayOrgin = char.HumanoidRootPart.Position
	local rayDirection = char.HumanoidRootPart.CFrame.LookVector * Vector3.new(0,0,11)
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {char}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local result = workspace:Raycast(rayOrgin,rayDirection,params)
	
	if result then
		print(result.Instance)
		local hit = result.Instance
		local hitp = result.Position
		print("This is what the hit position is: " .. tostring(hitp))
		if hit.CanCollide == false then return end
		return v == hit, p == hitp
	end
end

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		local hitpart, partpos = raycast()
		print(hitpart)
		print("This is what the return thinks it is: " .. tostring(partpos))
		if hitpart == nil then
			char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-distance)
			hitpart = nil
			for i = 1,10 do
				
			end
		elseif partpos ~= nil then
			char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(partpos.X,partpos.Y,partpos.Z)
		end
	end
end)

Okay i see the problem, im fixing in rn

1 Like

Right, so because you’re using the == operator to compare v to hit and p, those are going to return Booleans as well. (the == operator is used to see if a is equal/== to b).

You can just write it as return hit, hitp.

1 Like

Yeah, that’s what I did. Thanks for the help!

1 Like