This script should print a random players name who has played the game but it always errors and says ‘invalid argument #1 to ‘random’ (interval is empty)’
local service = game:GetService("DataStoreService")
local store = service:GetDataStore("PlayersWhoJoined")
local key = "MainList1"
local players = game.Players
players.PlayerAdded:Connect(function(player)
store:UpdateAsync(key, function(old)
old=old or {}
old[player.Name] = player.Name
return old
end)
local data = store:GetAsync(key)
local Randplayer = math.random(#data)
print(Randplayer)
end)
local service = game:GetService("DataStoreService")
local store = service:GetDataStore("PlayersWhoJoined")
local players = game.Players
local key = "MainList1"
players.PlayerAdded:Connect(function(player)
store:UpdateAsync(key, function(old)
old = old or {}
old[player.UserId] = true
return old
end)
end)
while true do
local data = store:GetAsync(key)
if data then
local playerIds = {}
for playerId in pairs(data) do
table.insert(playerIds, playerId)
end
if #playerIds > 0 then
local randomIndex = math.random(1, #playerIds)
local randomPlayerId = playerIds[randomIndex]
local randomPlayer = players:GetPlayerByUserId(randomPlayerId)
if randomPlayer then
print(randomPlayer.Name)
end
end
end
wait(60) -- maybe this will work
end