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

Why does this error when printing the value of an Vector3Value?

Asked by
OniiCh_n 410 Moderation Voter
9 years ago
local posTable = {"pos1", "pos2", "pos3", "pos4"}

for i = 1, 24 do
    n = math.random(1, #posTable)
    pos = posTable[n]
    newPos = game.ReplicatedStorage.positions:FindFirstChild(pos)
    print("posName: " .. newPos.Name)
    print("posValue: " .. newPos.Value)
end

As the title suggests, what is giving me this error?

I get the following output:

posName: pos3

22:18:09.602 - Workspace.Script:8: attempt to concatenate field 'Value' (a userdata value)

22:18:09.602 - Stack Begin

22:18:09.602 - Script 'Workspace.Script', Line 8

22:18:09.603 - Stack End

0
I have everything in the proper hierarchies, as evidenced by the "posName: " line printing properly. OniiCh_n 410 — 9y

1 answer

Log in to vote
2
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

You're probably used to being able to concatenate a string and a number in Lua.

print("Number: " .. 5)
--> 5

However, userdata isn't automatically converted to a string when concatenated. You have to use the tostring function.

print("Vector3: " .. Vector3.new(5,5,5))
--> error: attempt to concatenate a userdata value

print("Vector3: " .. tostring(Vector3.new(5,5,5)))
--> Vector3: 5, 5, 5

As an alternative, you can use print with a variatic number of arguments. This automatically converts each argument into a string.

print("Vector3: ", Vector3.new(5,5,5))
--> Vector3:  5, 5, 5
1
Thanks for the answer! I never knew about tostring() OniiCh_n 410 — 9y
Ad

Answer this question