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

How do I use brackets for variable names with functions that have parameters?

I am unsure what is happening here. Im pretty sure its the script.Parent.Size[axis], but how can i fix it?

local LiftCoefficient = 200

function FindArea(axis)
	return script.Parent.Size.X+script.Parent.Size.Y+script.Parent.Size.Z-script.Parent.Size[axis]
end

local LookVelocity = Vector3.new(script.Parent.CFrame.LookVector.X*script.Parent.Velocity.X,script.Parent.CFrame.LookVector.Y*script.Parent.Velocity.Y,script.Parent.CFrame.LookVector.Z*script.Parent.Velocity.Z)
	
	local Lift = math.max(FindArea("Y")*LookVelocity*LiftCoefficient,0) --bad argument #1 to 'max' (number expected, got Vector3)

Update: I modified the function, but still get same error

local LiftCoefficient = 200

function FindArea(axis)
	local area = 0
	if axis == "X" then
		area = script.Parent.Size.Z+script.Parent.Size.Y
	elseif axis == "Y" then
		area = script.Parent.Size.X+script.Parent.Size.Z
	elseif axis == "Z" then
		area = script.Parent.Size.X+script.Parent.Size.Y
	end
	print(area) -- > 64
	return area
end

	local LookVelocity = Vector3.new(script.Parent.CFrame.LookVector.X*script.Parent.Velocity.X,script.Parent.CFrame.LookVector.Y*script.Parent.Velocity.Y,script.Parent.CFrame.LookVector.Z*script.Parent.Velocity.Z)

	local Lift = math.max(FindArea("Y")*LookVelocity*LiftCoefficient,0) --bad argument #1 to 'max' (number expected, got Vector3)

3 comments

0

No, you don't use square brackets like that. Why do you need it?

0

i would like to find the property with the name equal to the same value as the axis string

0

actually nvm. the function actually works perfectly fine the first time, the problem was defining lift, "LookVelocity was a Vector3" and needed to be LookVelocity.Z

1 answer

✓ Accepted answer

The reason you are getting that error isn't because of the brackets. That works just the way it is. The reason for the error is that on this line:

local Lift = math.max(FindArea("Y")*LookVelocity*LiftCoefficient,0)

you are trying to find the maximum value between a Vector3 and a number. math.max only accepts number values. So in order to fix this, you should find the maximum value between the magnitude of the Vector3 and 0

local Lift = math.max((FindArea("Y")*LookVelocity*LiftCoefficient).Magnitude,0)

1 comment

0

Yeah, I changed it to LookVelocity.Z. Thanks!

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