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

DataStore ban system using keys?

So I'm writing a system for my moderators to be able to easily view bans and then reason, date of ban and etc..

Here's my current script

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("PlayerInfo")

game.Players.PlayerAdded:connect(function(plr)
	local key -- where I'm having issues
	local moderation = Instance.new("Folder", plr)
	moderation.Name = "Moderation"
	local baninfo = Instance.new("BoolValue", moderation)
	baninfo.Name = "Banned"
	local DOB = Instance.new("StringValue", moderation)
	DOB.Name = "DateOfBan"
	local BB = Instance.new("StringValue", moderation)
	BB.Name = "BannedBy"
	local UI = Instance.new("StringValue", moderation)
	UI.Name = "UserId"
	UI.Value = plr.userId
	local nam = Instance.new("StringValue", moderation)
	nam.Name = "Username"
	local RS = Instance.new("StringValue", moderation)
	RS.Name = "Reason"
	
	local getSaved = DataStoreService:GetAsync(key)
	if getSaved then
		baninfo = getSaved[1]
	else
		local NumbersForSaving = {baninfo.Value}
		DataStoreService:SetAsync(key, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local key -- also where I'm having issues
	local saveTable = {plr.moderation.Banned.Value}
	DataStoreService:SetAsync(key, saveTable)
end)

what I want is to be able to use a key and then all the banned players names come up and then open up their name and see reason, date of ban, bannedby, and userId

I'm using Crazyman32's DataStore Editor as a way of viewing of the DataStore if that helps

4 comments

0

What do you mean by "All the banned players names come up"? Do you mean in a GUI?

0

yeah basically like if I enter a Datastore Key then it shows up a table of all the players and then be able to see a table of all the players data

0

This is just a script for saving those values. Did you attempt creation of a GUI?

0

I wouldn't know where to start also I'm mainly just looking for a way of storing everything under 1 key and being able to see the playersa info

1 answer

✓ Accepted answer

So if I understand correctly, you simply want to store a table of bans into a datastore containing information about why they were banned, who they were banned by, date they were banned etc.

I would use something along the lines of:

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerInfo")
local Banlist = {} --A table which we will use to store our data
local Key = "Banlist" --A generic key which we will store all of our data under

game.Players.PlayerAdded:connect(function(Player) --Will fire when player joins
	
	Banlist = Datastore:GetAsync(Key) --Will update the list every time someone joins

	for _,Bans in pairs(Banlist) do --Loop through the banlist
		if (Bans['userId'] == Player.userId) then --If they are already banned then...
			Player:Kick("You are banned from the server") --Kick the player
			return --Stop the function from continuing
		end
	end

	local moderation = Instance.new("Folder", Player) --You know this part...
	moderation.Name = "Moderation"
	local baninfo = Instance.new("BoolValue", moderation) --Are they banned?
	baninfo.Name = "Banned"
	local DOB = Instance.new("StringValue", moderation) --When were they banned?
	DOB.Name = "DateOfBan"
	local BB = Instance.new("StringValue", moderation) --Who banned them?
	BB.Name = "BannedBy"
	local UI = Instance.new("StringValue", moderation) --Guy who banned them's userId
	UI.Name = "UserId"
	local RS = Instance.new("StringValue", moderation) --Reason for ban?
	RS.Name = "Reason"

end)

game.Players.PlayerRemoving:connect(function(Player) --Will fire when the player leaves

	if (Player.Moderation.Banned.Value) then --If they were banned

		local Moderation = Player.Moderation

		Datastore:UpdateAsync(Key, function(Bans) --Update the datastore ("Bans" is the list already stored)
	
			table.insert(Bans, {
				['userId'] = Player.userId;
				['DateOfBan'] = Moderation.DateOfBan.Value;
				['BannedBy'] = Moderation.BannedBy.Value;
				['BannerUserId'] = Moderation.UserId.Value;
				['Reason'] = Moderation.Reason.Value;
			})  --Add the ban to the banlist

			return Bans --Update the list
	
		end)

	end

end)

There are better ways of doing this, but I don't have much time. If you have questions, please drop a PM or comment.

If you want help with the datastores, or techniques which I have used here, check my answer on this question.

This script only handles the saving of the data, if you want to ban/unban them then you will need to write separate code

2 comments

0

Thats fine you've been a big help!

0

There's an error on line 9 it says "bad argument #1 to 'pairs' (table expected, got nil)

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