Vector3Value making 124 reduce itself

Script :

print(wigea.Value.X)
print(game.Players:GetPlayerFromCharacter(playa).UserId)
wigea.Value += Vector3.new(game.Players:GetPlayerFromCharacter(playa).UserId,0,0)
print(wigea.Value.X)

Result :

 --0  - sgrebinm:510
 --1043034904  - sgrebinm:511
<added>
 --1043034880  - sgrebinm:513

Ok where’s 124?

I know only this method to add value at Vector3Value.
Tell me if there’s what I wrong or another method to add value. I will try it.
This dam bug made my game release late

This is a floating point precision problem. The number type in Luau is a 64-bit floating point number but Vector3 components are stored as 32-bit floating point numbers. When you assign values to the components, they are rounded to numbers that can be represented as 32-bit floating point numbers. So you often lose precision when using Vector3s.

Floating point numbers are denser near zero. This means that the bigger (in absolute value) the values you work with are, the bigger are the gaps between values that can be represented using a floating point number with a spesific number of bits. UserIds are quite big numbers, so, as can be seen here, the closest 32-bit number may be quite far from the original number. 64-bit floating point numbers can represent billions of times more numbers than 32-bit numbers (2^32 = 4 294 967 296) so they are a lot more dense at the magnitude of UserIds and thus can be used for representing UserIds accurately while 32-bit floating point numbers can’t.

Also, the difference between those two numbers is 24, not 124 :wink:.

Why are you even storing UserIds into vectors? If you need to do vector operations on them, you can write your own vector class or alternatively look for an opensourced one. That way you’ll have vectors that use 64-bit floating point numbers. If you don’t need to use vector operations on them, you can probably use an array or a dictionary instead depending on your use case.

1 Like

So userid cant be used in Vector3Value in any method?
Thing to edit got more… including my brain that calculated 24 as 124

Yes, you can’t store a UserId accurately in a Vector3Value. I can’t think of a reason why you would even do that, though. Why does it need to be stored in a vector?

It would be more efficient to store and write script.
That vectorvalue saved userid in X, user’s score in Y, user’s team in Z.
But now I should make 2+ numbervalues

You could alternatively use instance attributes if you need automatic replication and changed events. You can get a changed event for a spesific attribute with Instance:GetAttributeChangedSignal(). If you don’t need changed events or automatic replication, you can use a dictionary. And even if you do need automatic changed events and replication, you can implement those for tables yourself with metatables if you have the knowledge required for that. You’ll need an empty table with a metatable that has __index and __newindex metamethods, another table that contains the actual data, a custom event class and a remote event. This way you can have a table which you can index normally to get or set values but which will also automatically fire a changed event and replicate the change. I won’t explain this further here so use value objects or attributes if this is too complicated and you need changed events and replication.

In addition to being more flexible, the alternative approaches allow you to store each value using a name that actually describes the value instead of storing them as X, Y and Z of a vector.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.