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)
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
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)
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).