1 | 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.*) |
01 | function SplitEarth(Pos, OldPrt, Type, Params) |
02 | local Base = OldPrt.CFrame.Y - (OldPrt.Size.Y/ 2 ) |
03 | local Top = OldPrt.CFrame.Y + (OldPrt.Size.Y/ 2 ) |
04 |
05 | if Type = = 1 then -- [ Horizontal split. ] |
06 | -- [ Below is my problem, I don't see any indexing here... ] |
07 | local PosY 1 = Pos + ((Top - Pos).magnitude/ 2 ) -- [ The first Y value, for the part on top. ] |
08 | local PosY 2 = Base + ((Base + Pos).magnitude/ 2 ) -- [ The second Y value. ] |
09 | --[[local NewY1 = ().magnitude --[ I'll work on these two later. ] |
10 | local NewY2 = ().magnitude |
11 | local Prt1 = OldPrt:clone() |
12 | Prt1.Size = Vector3.new(OldPrt.Size.X, NewY1, OldPrt.Size.Z) |
13 | Prt1.Position = Vector3.new(OldPrt.CFrame.X, PosY1, OldPrt.CFrame.Z) |
14 | Prt1.Parent = OldPrt.Parent |
15 | local Prt2 = OldPrt:clone() |
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:
1 | local PosY 1 = 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:
1 | return PosY 1 , PosY 2 |
When calling the function, you'd then receive the returned values like-so:
1 | someY 1 , someY 2 = SplitEarth( --[[arguments]] ) |