Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Indexing a Number value error? When the value isn't being indexed?

Asked by
Xetrax 85
9 years ago
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

0
Lol, idk how, but I guess it made my main question a part of the code... whoops, I also just now noticed a spelling error of mine in a comment... Xetrax 85 — 9y
0
Fixed my spelling error... :| Xetrax 85 — 9y
0
Why are you putting [ ] around your comments? Also -- can you quote the *actual, full error* rather than paraphrase? I think you've misunderstood it. BlueTaslem 18071 — 9y
0
Putting the brackets around my comments is just a style of mine, and I'll add the full error here: 23:16:40.920 - Workspace.FractureTest.Break:31: attempt to index a number value 23:16:40.920 - Stack Begin 23:16:40.921 - Script 'Workspace.FractureTest.Break', Line 31 - global SplitEarth 23:16:40.921 - Script 'Workspace.FractureTest.Break', Line 55 23:16:40.921 - Stack End Xetrax 85 — 9y
View all comments (6 more)
0
If the number value that you are trying to index (read) is 'Pos', and you're getting that error, then 'Pos' is simply not a number. There is no fault in this function, but rather a problem with the arguments that you are supplying to it from elsewhere. duckwit 1404 — 9y
0
I thought of that, so I changed the name of "Pos", yet it still threw me the same error. and Pos is a number, I have triple checked, the error is thrown when a value is indexed in the way a table or string would be referenced, but Pos is a number value, and this is why the error is confusing me. Xetrax 85 — 9y
0
Changing the name doesn't affect anything, as long as the same name is used consistently throughout the script. You have triple checked? Find the exact line where the error occurs, note all the index operations that take place (this includes referencing a variable). Now, before that line, use individual print() statements to see what the values are . You will find which values are erroneous. duckwit 1404 — 9y
0
Thanks, I'll try this shortly, but one last thing, I did that... aaand I even printed the type() of the Pos variable (number) and the result of the formula I'm trying to get to work, the error happens when I try to include Pos in the formula, it just stops dead... Xetrax 85 — 9y
0
I see now. Top and Pos are numbers, but not vectors. You're trying to calculate (Top - Pos).magnitude. I think you meant simply '(Top - Pos)/2'. duckwit 1404 — 9y
0
Ahhh, Thank you. Xetrax 85 — 9y

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
9 years ago

Indexing the Number

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.

Returning

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]])
0
Thanks, this will help me a lot, the game I'm making depended on this working for a major feature... Xetrax 85 — 9y
Ad

Answer this question