Can someone please help me with this function, Every time I compile it, it throws *"attempt to index a number value"* for the "Pos" variable, and idk how to fix this, again, can someone please help **ASAP**... (*The function splits a part in two from a midpoint based from the Pos value.*)
function SplitEarth(Pos, OldPrt, Type, Params) local Base = OldPrt.CFrame.Y - (OldPrt.Size.Y/2) local Top = OldPrt.CFrame.Y + (OldPrt.Size.Y/2) if Type == 1 then --[ Horizontal split. ] --[ Below is my problem, I don't see any indexing here... ] local PosY1 = Pos + ((Top - Pos).magnitude/2) --[ The first Y value, for the part on top. ] local PosY2 = Base + ((Base + Pos).magnitude/2) --[ The second Y value. ] --[[local NewY1 = ().magnitude --[ I'll work on these two later. ] local NewY2 = ().magnitude local Prt1 = OldPrt:clone() Prt1.Size = Vector3.new(OldPrt.Size.X, NewY1, OldPrt.Size.Z) Prt1.Position = Vector3.new(OldPrt.CFrame.X, PosY1, OldPrt.CFrame.Z) Prt1.Parent = OldPrt.Parent local Prt2 = OldPrt:clone() Prt2.Size = Vector3.new(OldPrt.Size.X, NewY2, OldPrt.Size.Z) Prt2.Position = Vector3.new(OldPrt.CFrame.X, PosY2, OldPrt.CFrame.Z) Prt2.Parent = OldPrt.Parent]] elseif Type == 2 then --[ Vertical split. ] return elseif Type == 3 then --[ Diagonal split. ] return --[ I have an idea how to to this, but I do welcome suggestions... ] end end --[ 1 = horiz. split, 2 = vert. split, 3 = diag. split, 4 = chunk split. ]
If you can help, and soon, I will be very grateful; Regards, Xetrax
On line 7 you write (Top - Pos).magnitude/2
. Mathematically, the magnitude of a one-dimensional value is simply the positive version of that value. For example, the magnitude of -7 is 7, -10 is 10, and the magnitude of 3 is 3.
The magnitude of a multi-dimensional vector is the distance of that point from the origin (the point where, on each axis, the value is 0, for example (0,0,0)
for 3-dimensional space).
You seem to be mixing these two ideas computationally; Top
and Pos
are not vectors, so you don't need to perform any sort of operation to use their value. You actually want to write:
local PosY1 = Pos + (Top - Pos)/2
The error arises because Lua tries to index the number value Top - Pos
, as if it acted as a table and had an entry for 'magnitude'.
Edit: If you do want to calculate the magnitude of a number, the math
library in Lua has a function for that, abs
, which stands for 'absolute value'. For example, math.abs(-7)
will output 7
.
You wrote a comment on line 22 which I'll also address. In Lua, you can return multiple values by separating them with commas. In your case:
return PosY1, PosY2
When calling the function, you'd then receive the returned values like-so:
someY1, someY2 = SplitEarth(--[[arguments]])