So, im working on a game that involves user created content and while working on this feature i couldn’t think of a way to pick a random player from the datastore, to check if they have any saved data and return it.
Hi!
It looks like you’re trying to use the datastoreservice to store and retrieve user data.
This can be done with a script, as follows:
local dss = game:GetService("DataStoreService")
local userdatastore = dss:GetDataStore("user_data")
local succ, err = pcall(function()
data = userdatastore:GetAsync(playerid)
end)
if succ and data ~= nil then print(data) return end
This is a quick script to print any data a user has any saved in the datastore.
Here’s some documentation from Roblox regarding datastores: DataStoreService
Additionally, here are two YouTube videos I recommend as resources to learn more about how to use them:
Hope this helps, have fun and good luck!
You cannot just 'get a random player` as far as I’m aware. You can onlt get the information for a player that you have the ID for.
You could use systems such as OrderedDataStores, or just storing a list of players in another datastore, but both of these solutions have many issues. I don’t see why exactly you’d need to get a random player’s data though ¯\(ツ)/¯
I know how to use datastores already, my question was if i could get all the ids in the datastore so i could pick a random one out of those.
You can definitely make a system to pick a random player. Just off the top of my head, something like: Math.random(1,#players)
Uhh, what? What is players
? Is it a list of players in the game, which should work, but I’m not sure if thats what the OP is asking for. Is it the amount of accounts on Roblox, which definately, would not work on the other hand.
I wasn’t using “players” as an actual item, more of a frame of reference. With that you’d be able to pick a random number based on the amount of players stored in their datastore / list, and from there you can pick that item from the list given the number generated.