How do I get every User-Name from a DataStore?

Hello guys,
I have made a ban players script that works with a kick system when a players joins it checks if the bandatastore is for him true, if its true then kick him instantly. Everytime a player gets banned, it will make the bandatastore to true for him. I have now scriped a list banned players with a discord webhook. But that doesnt work.Here’s my script (ServerScriptService):

local DataStoreService = game:GetService("DataStoreService")
local BanDataStore = DataStoreService:GetDataStore("BannedPlayers")
local HttpService = game:GetService("HttpService")

local rp = game:GetService("ReplicatedStorage")
local events = rp:FindFirstChild("Events")
local webhooks = rp:FindFirstChild("Informations"):FindFirstChild("Webhooks")
local banWebhook = webhooks:FindFirstChild("Admin").Value
local groupID = rp:FindFirstChild("Informations"):FindFirstChild("groupID").Value

local function HasRole(player, roleName)
	local success, roleInfo = pcall(function()
		return game:GetService("GroupService"):GetGroupInfoAsync(groupID)
	end)
	if success then
		for _, role in ipairs(roleInfo.Roles) do
			if role.Name == roleName then
				return player:IsInGroup(groupID, role.Id)
			end
		end
	end
	return false
end

local function HasAnyRole(player, roleNames)
	for _, roleName in ipairs(roleNames) do
		if HasRole(player, roleName) then
			return true
		end
	end
	return false
end

local adminRoles = {
	"「👑」Owner",
	"「🔥」Co-Owner",
	"「⚒️」Admin",
}


local function SendWebhook(embed)
	local payload = {
		embeds = {embed}
	}
	HttpService:PostAsync(banWebhook, HttpService:JSONEncode(payload), Enum.HttpContentType.ApplicationJson)
end

local function GetBannedPlayers()
	local success, bannedPlayers = pcall(function()
		return BanDataStore:GetAsync("BannedPlayers")
	end)
	if success then
		return bannedPlayers or {}
	else
		warn("Error retrieving banned players:", bannedPlayers)
		return {}
	end
end



local function ListBannedPlayers()
	local bannedPlayers = GetBannedPlayers()
	print("Retrieved banned players:", bannedPlayers)
	
	local bannedPlayers = GetBannedPlayers()
	if #bannedPlayers == 0 then
		local embed = {
			title = "No Banned Players",
			color = tonumber(0x00ff00),
			description = "There are no players currently banned."
		}
		SendWebhook(embed)
	else
		local embed = {
			title = "List of Banned Players",
			color = tonumber(0xff0000),
			description = "The following players are banned:",
			fields = {}
		}
		for _, playerName in ipairs(bannedPlayers) do
			table.insert(embed.fields, {
				name = "Player:",
				value = playerName,
				inline = true
			})
		end
		SendWebhook(embed)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if HasAnyRole(player, adminRoles) then
			if message:lower() == "!listbanned" then
				ListBannedPlayers()
			end
		end
	end)
end)

Thanks for your help!

I have a question, when the player gets banned, does their name get saved in the BannedPlayers list? Also, there’s an issue with two parts of your code, unrelated to the error.

DataStores have a 4MB limit, so it can only store 200,000 - 1,333,333 players before it stops working. Also, storing webhooks in ReplicatedStorage can cause exploit issues as all clients can see in ReplicatedStorage, so they can steal your webhooks from it.

The webhook won’t work if it’s a Discord webhook. Discord have blocked webhook requests from Roblox due to misuse; you need to use a proxy if you want to use them.

You could just make an individual entry for each player to the store, and use ListKeysAsync() to get the names that way.

Hello, I have rescripted it and changed it to player.UserId, here’s my new script:

local DataStoreService = game:GetService("DataStoreService")
local BanDataStore = DataStoreService:GetDataStore("BannedPlayers")
local HttpService = game:GetService("HttpService")

local rp = game:GetService("ReplicatedStorage")
local events = rp:FindFirstChild("Events")
local webhooks = rp:FindFirstChild("Informations"):FindFirstChild("Webhooks")
local banWebhook = webhooks:FindFirstChild("Admin").Value
local groupID = rp:FindFirstChild("Informations"):FindFirstChild("groupID").Value

local function HasRole(player, roleName)
	local success, roleInfo = pcall(function()
		return game:GetService("GroupService"):GetGroupInfoAsync(groupID)
	end)
	if success then
		for _, role in ipairs(roleInfo.Roles) do
			if role.Name == roleName then
				return player:IsInGroup(groupID, role.Id)
			end
		end
	end
	return false
end

local function HasAnyRole(player, roleNames)
	for _, roleName in ipairs(roleNames) do
		if HasRole(player, roleName) then
			return true
		end
	end
	return false
end

local adminRoles = {
	"「👑」Owner",
	"「🔥」Co-Owner",
	"「⚒️」Admin",
}


local function SendWebhook(embed)
	local payload = {
		embeds = {embed}
	}
	HttpService:PostAsync(banWebhook, HttpService:JSONEncode(payload), Enum.HttpContentType.ApplicationJson)
end

local function GetBannedPlayers()
	local bannedPlayers = {}
	local success, error = pcall(function()
		local players = game.Players:GetPlayers()
		for _, player in ipairs(players) do
			if BanDataStore:GetAsync(player.UserId) == true then
				table.insert(bannedPlayers, player.Name)
			end
		end
	end)
	if not success then
		warn("Error retrieving banned players:", error)
	end
	return bannedPlayers
end



local function ListBannedPlayers()
	local bannedPlayers = GetBannedPlayers()

	if #bannedPlayers == 0 then
		local embed = {
			title = "No Banned Players",
			color = tonumber(0x00ff00),
			description = "There are no players currently banned."
		}
		SendWebhook(embed)
	else
		local embed = {
			title = "List of Banned Players",
			color = tonumber(0xff0000),
			description = "The following players are banned:",
			fields = {}
		}

		for _, playerName in ipairs(bannedPlayers) do
			local player = game.Players:FindFirstChild(playerName)
			if player then
				local playerId = player.UserId
				table.insert(embed.fields, {
					name = "Player:",
					value = playerName .. " (" .. playerId .. ")",
					inline = true
				})
			end
		end

		SendWebhook(embed)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if HasAnyRole(player, adminRoles) then
			if message:lower() == "!listbanned" then
				ListBannedPlayers()
			end
		end
	end)
end)

It stil doesn’t work. Everytime i write !listbanned, a webhook will be sent but it says No banned players everytime.

The webhook works fine. If I use ListKeysAsync() how would I need to rewrite my script? Thanks

For each entry, it has a key (obviously). In this example, I’m going to use the key “Key_PLAYERUSERID” (with a number to be the user id).

In your system, the player can change their name to get unbanned.

local players = game:GetService("Players")

local success, result = pcall(function()
    return banDataStore:ListKeysAsync()
end)
if not success then return nil end
for key, value in ipairs(result:GetCurrentPage()) do
    local key = tonumber(string.split(key, "_")[2])
    local success, result = pcall(players.GetNameFromUserIdAsync, players, key)
    if success then
        print(result) --this should be the banned player's name
    end
end

Bare in mind you need to make an entry for each banned player.
banDataStore:SetAsync("Key_"..player.UserId, true)

Just out of curiosity, did you need a proxy to get the webhook to work?

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