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

Why does this script can save but not load the data?

local DatastoreService = game:GetService("DataStoreService")
local ds = DatastoreService:GetDataStore("BanList")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event1 = ReplicatedStorage.AdminGui:WaitForChild("Ban")
local event2 = ReplicatedStorage.AdminGui:WaitForChild("Unban")

local Players = game:GetService("Players")

local webhook = ""
local HttpService = game:GetService("HttpService")

local admins = {
	"MythsList",
	"kindHeartedDa"
}

local bannedpeople = {}

ds:GetAsync(bannedpeople)

game.Players.PlayerAdded:Connect(function(plr)
	--ds:GetAsync(plr.UserId, bannedpeople)
	if table.find(bannedpeople, plr.UserId) then
		wait(0.2)
		plr:Kick("You are permanently banned from the game.")
		print("autoban")
	end
end)

local function Banned(admin, username, reason)
	local bannedplayer = Players:FindFirstChild(username)
	for i = 1, #admins do
		if admin.Name == admins[i] then
			if bannedplayer ~= nil then
				bannedplayer:Kick(reason)
				table.insert(bannedpeople, bannedplayer.UserId)
				ds:SetAsync(bannedplayer.UserId, bannedpeople)
				local data = {
					['embeds'] = {{
						['title'] = "Username : "..username,
						['description'] = "Reason : "..reason,
						['color'] = 0
					}}
				}
				local finalData = HttpService:JSONEncode(data)
				HttpService:PostAsync(webhook, finalData)
				print("banned")
			end
		end
	end
end

local function Unbanned(admin, username, reason)
	local unbannedplayer = Players:FindFirstChild(username)
	for i = 1, #admins do
		if admin.Name == admins[i] then
			if unbannedplayer ~= nil then
				if table.find(bannedpeople, unbannedplayer.UserId)then
					table.remove(bannedpeople, table.find(bannedpeople, unbannedplayer.UserId))
					ds:SetAsync(unbannedplayer.UserId, bannedpeople)
					local data = {
						['embeds'] = {{
							['title'] = "Username : "..username,
							['description'] = "Reason : "..reason,
							['color'] = 0
						}}
					}
					local finalData = HttpService:JSONEncode(data)
					HttpService:PostAsync(webhook, finalData)
					print("unbanned")
				end
			end
		end
	end
end

event1.OnServerEvent:Connect(Banned)
event2.OnServerEvent:Connect(Unbanned)

I still have problems with this

ds is the datastore

1 answer

:GetAsync only has the key as a parameter, and we only want ONE key for the banned list, or else there will be different ban lists, each for one player with different players. You obviously didn't fully read my last answer, so let me fix that script for you:

local DatastoreService = game:GetService("DataStoreService")
local ds = DatastoreService:GetDataStore("BanList")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event1 = ReplicatedStorage.AdminGui:WaitForChild("Ban")
local event2 = ReplicatedStorage.AdminGui:WaitForChild("Unban")

local Players = game:GetService("Players")

local webhook = ""
local HttpService = game:GetService("HttpService")

local admins = {
	"MythsList",
	"kindHeartedDa"
}

local bannedpeople = {}

game.Players.PlayerAdded:Connect(function(plr)
	
	pcall(function() -- pcall because it can error and then everyone will be unbanned as bannedpeople will be nil
		
		bannedpeople = ds:GetAsync("banned")
		
	end)
	
	if table.find(bannedpeople, plr.UserId) then
		
		wait(0.2)
		plr:Kick("You are permanently banned from the game.")
		print("autoban")
		
	end
	
end)

local function Banned(admin, username, reason)
	
	local bannedplayer = Players:FindFirstChild(username)
	for i = 1, #admins do
		if admin.Name == admins[i] then
			if bannedplayer ~= nil then
				bannedplayer:Kick(reason)
				table.insert(bannedpeople, bannedplayer.UserId)
				ds:SetAsync("banned", bannedpeople)
				local data = {
					['embeds'] = {{
						['title'] = "Username : "..username,
						['description'] = "Reason : "..reason,
						['color'] = 0
					}}
				}
				local finalData = HttpService:JSONEncode(data)
				HttpService:PostAsync(webhook, finalData)
				print("banned")
			end
		end
	end
end

local function Unbanned(admin, username, reason)
	local unbannedplayer = Players:FindFirstChild(username)
	for i = 1, #admins do
		if admin.Name == admins[i] then
			if unbannedplayer ~= nil then
				if table.find(bannedpeople, unbannedplayer.UserId)then
					table.remove(bannedpeople, table.find(bannedpeople, unbannedplayer.UserId))
					ds:SetAsync("banned", bannedpeople)
					local data = {
						['embeds'] = {{
							['title'] = "Username : "..username,
							['description'] = "Reason : "..reason,
							['color'] = 0
						}}
					}
					local finalData = HttpService:JSONEncode(data)
					HttpService:PostAsync(webhook, finalData)
					print("unbanned")
				end
			end
		end
	end
end

event1.OnServerEvent:Connect(Banned)
event2.OnServerEvent:Connect(Unbanned)

Paste that code in without changes (only your webhook id) and it will work.

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