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

Generate unique ID for a community system?

Asked by
Turgon 80
9 years ago

Hi, im making a community system that will use datastores. I need to save them to datastores with unique ID's as keys but i'm not sure about how i can generate an unique number. I've thought about using tick() and math.random but i have no idea if it will be unique in each instance of the game aswell. Please help?

0
something with metatables. HungryJaffer 1246 — 9y

4 answers

Log in to vote
1
Answered by
NotSoNorm 777 Moderation Voter
9 years ago

Well, I don't really think you should make it 'random' per say, But I think you should make the ID's a giant list, then save it with a thing called OrderedDataStore: http://wiki.roblox.com/index.php?title=Ordered_data_store

With Ordered data stores you can get the next value up from the last save, I'm not sure how it'd work though. Goodluck m80

0
That's actually possible. I don't really know how OrderedDataStore works but i'll figure it out. Turgon 80 — 9y
Ad
Log in to vote
4
Answered by 9 years ago

Why don't you use the Player's UserId since that is unique to every user?

0
Because communites aren't specific to players, the members can change. If i used the creator's user id and then the creator left to create another one, they would overlap. Turgon 80 — 9y
3
Please elaborate, Turgon? Members can change but how would UserIds overlap when we already said they're unique? Marios2 360 — 9y
0
Im assuming that the community's ID will be the the UserId of the player who created it, if that person was to leave the group and make another another one, there would be 2 groups with the same ID; the groups aren't deleted if the creator leaves Turgon 80 — 9y
0
If the creator leaves his own group and there's nobody in it anymore, why not remove it? And if there's anyone in there. make owner the person who has been on the group the most. Marios2 360 — 9y
Log in to vote
1
Answered by
yumtaste 476 Moderation Voter
9 years ago

As you probably know, math.random() isn't truly random. If you do math.random() in test mode, quit, then do it again, you notice you get the same/very similar numbers. You can reset the seed that math.random() uses by doing math.randomseed(tick()). This will set the seed to how many seconds have passed since January 1st, 1970 (strange, yes, but useful). So, your code would look something like this:

math.randomseed(tick())
getCommunityNumber(math.random())
0
Why January 1st, 1970? ISellCows 2 — 9y
0
According to Wikipedia, it's the Epoch. Not 100% sure what that means, but I think it was created for a reference point. https://en.wikipedia.org/wiki/Unix_time yumtaste 476 — 9y
0
I actually did this but it gives the same number if it's called at similar times, if a person creates a crew and another person creates a crew a few seconds later they get the same ID. Do i have to check every id in the datastore one by one? That would be troublesome and probably take a while. (If possible, heh) Turgon 80 — 9y
Log in to vote
-1
Answered by 9 years ago

Maybe you should generate key with random() and then check is this key already in Data Store. If it is then do random() again until you get unique key.

Answer this question