How I make a Admin Script? Part 2

yes, i dont want my moderators always join in a random server and ban and unban them

Okay! I’ll post the script here when I’m done!

is been 11h hours… where is it?

@zrax_rb

--// Services \\--
local DSS = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MessagingService = game:GetService("MessagingService")

--// Variables \\--
local Admins = require(script.Parent.Settings).Ranks

--// DataStores \\--
local BannedPlayers = DSS:GetDataStore("BannedPlayers")

--// Functions \\--
function IsAdmin(player)
	for _, v in pairs(Admins) do
		if player.UserId == v then
			return true
		end
	end

	return false
end

function Time()
	
end

--// Script \\--

game.Players.PlayerAdded:Connect(function(player)
	if IsAdmin(player) == true then
		local ui = script.AdminGUI:Clone()
		ui.Parent = player.PlayerGui
	end
	player.PlayerGui.ChildAdded:Connect(function(child)
		if child:IsA("ScreenGui") and child.Name == script.AdminGUI.Name and IsAdmin(player) == false then
			player:Kick("Reason: \n Exploiting to AdminScreen. \n Kicked By System")
		end
	end)
end)

ReplicatedStorage.Remotes.Kick.OnServerEvent:Connect(function(plr, reason)
	plr:Kick("You've Been Kicked \n Reason: \n"..reason)
end)

ReplicatedStorage.Remotes.Ban.OnServerEvent:Connect(function(plr, reason, duration)
	plr:Kick("You've Been Banned \n Reason: \n"..reason.."\n Duration: "..duration)
	BannedPlayers:SetAsync(plr.UserId,{
		['Name'] = plr.Name,
		['UserId'] = plr.UserId
	})
	MessagingService:PublishAsync("BANNED_PLAYERS")
end)

is this script good?

Hey, mind if you show your banning gui?

sure:

First, in the ban event, save the ban first with pcalls and if the request is sent and everything is fine then you would kick them. I don’t really know what is the purpose of the publishasync function because you already kicked the player.

How so? so like this:

ReplicatedStorage.Remotes.Ban.OnServerEvent:Connect(function(plr, reason, duration)
	plr:Kick("You've Been Banned \n Reason: \n"..reason.."\n Duration: "..duration)
	local s, e  = pcall(function()
		BannedPlayers:SetAsync(plr.UserId,{['Name'] = plr.Name,['UserId'] = plr.UserId})
	end)
	if not s then TestService:Error(e)
end)
ReplicatedStorage.Remotes.Ban.OnServerEvent:Connect(function(plr, reason, duration)
	
	local s, e  = pcall(function()
		BannedPlayers:SetAsync(plr.UserId,{['Name'] = plr.Name,['UserId'] = plr.UserId})
	end)
	if not s then 
        error(e) 
        else
         plr:Kick("You've Been Banned \n Reason: \n"..reason.."\n Duration: "..duration)
     end
end)

However, the code above is purely for one-server communications aka, it doesn’t support the following concept “A kick request is sent → The player isn’t in the same server you are in → The system sends a messaging service request → the player is kicked” but you can achieve that with messaging service though.

but if the player joins again he doenst get kicked? banned players cant access the game until they are unbanned

Huh? What are you exactly doing with the event? Is it fired everytime the ban button is pressed or when a player joined the game?

Edit:
If it is fired every time the button is pressed then the code is fine (mostly, there isn’t any checks to stop multi-write requests though).

If it is fired every time a player joins the game then you would use the Players.PlayerAdded event and a getasync usage.

When the event is fired, he will get the info script send to him.
resulting: kicking the player

I don’t understand you. When do you fire that event?

i mean, when the event gets fired he will kick the player and when he trys again join he will get kicked by the script and no is not every time a player joins. is every time the ban script fire it.

And when the ban script fires it?

no, the ban script dont fire it, a button fires it that the script gets information like duration, reason and player.

Then that usage will only save the ban, aka, the banned player won’t get kicked when he joins another server.

so how do i save the ban?
if he try to get another server i should kick him

You already saved the ban when using that event. However, if you want to kick the player when he joins the player again you would try the following:

game.Players.PlayerAdded:Connect(function(plr)
         local Data
      local s, e  = pcall(function()
		Data = BannedPlayers:GetAsync(plr.UserId)
	end)

       if s and Data then -- if the request is successful and the data is valid.
              plr:Kick()
          elseif not Data then
                print("No data found")

           else
            error(e)
       end
 end)

Again, do your research on Datastores and Roblox server-client model because you seem to not understand it very well (from what I can see in the past replies),

yeah, im a starter dev i should have learned in developer hub