Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Generate unique ID for a community system?

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?

1 comment

0

something with metatables.

4 answers

✓ Accepted answer

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

1 comment

0

That's actually possible. I don't really know how OrderedDataStore works but i'll figure it out.

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

4 comments

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.

3

Please elaborate, Turgon? Members can change but how would UserIds overlap when we already said they're unique?

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

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.

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())

3 comments

0

Why January 1st, 1970?

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

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)

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.