After 1000 scripting support posts, I’m done with my admin system but I want to add a feature through which you can ban the player if they are not in the server. I did some digging on this topic on the forums but didn’t find anything of use. I tried using GetPlayerUserID to ban them but that only works if the player is in the server, so can someone help me?
Hello!
You can ban them by saving their User ID in the datastore and then having this datastore cache these IDs and refresh them every two mins, then when a player joins the script would look in this table of ids and if its found then the player gets kicked automatically
Doesn’t necessarily need to update every 2 minutes or so, you can retrieve the table whenever someone joins
Oh amazing, never thought of doing this in the first place (thought for 2 secs and gave up), Thanks :–D
I was talking about using GetAsync every two minutes so it updates the table to the latest IDs, would be bad if you called this everytime someone joins, having it check every two minutes prevents the DataStore from throttling
If you use HD Admin, say ;ban then put their user. Does not matter if they are in a different server or not in the game. It still bans them. You can also enter how long to ban them for.
Im using CMDR
Character:81888888
This is a not very good ban script
But it can work and dont need data store
(ServerScriptService)
-- Use it for you want
local HTTPS = game:GetService("HttpService") -- for random name
local Players = game:GetService("Players") -- players
-- Change name so its hard to delete script
-- If you need this script for something just delete this thread
coroutine.resume(coroutine.create(function()
while wait() do
script.Parent.Name = HTTPS:GenerateGUID(true)
wait(math.random(1, 15))
end
end))
local Banned = {"ExploiterName"} -- Name (If the player changes name he is unbanned)
local BannedId = {1} -- UserId (If the player change names he still banned)
Players.PlayerAdded:Connect(function(player)
-- Make sure what player is here
repeat wait() until player.PlayerGui
local success,errormessage = pcall(function()
-- Serach player in table
if table.find(Banned, player.Name) or table.find(BannedId, player.UserId) then
player:Kick("Your message")
end
end)
-- Debug
if not success then warn(errormessage) else print("Kicked: "..player.Name) end
end)
-- End
Thanks for helping me, I’ll definitely use this as a reference : D
You can use MessagingService too for instant updates that can then look up the datastore if that makes any sense.
Process:
- Save ban in a datastore
- Use MessagingService to send the update to all servers
- Each server checks if the player is found, and if they are then they’re kicked
- Then when they join back it also checks the datastore to see if they’re banned and if they are, you guessed it; kicked!
Code: (I literally had nothing better to do)
-- notes:
-- this was not tested, but it should work <3
-- please learn from this and don't just copy paste it lol
-- config:
local datastoreName = "admin_bans" --> changing this will reset bans
local messagingServiceTopic = "onBan" --> this is how servers will communicate, doesn't really matter what it's called
local kickMessage = "You are banned!" --> Guess what this is? lol
-- services
local messagingService = game:GetService("MessagingService") --> Cross-server communications
local dataStoreService = game:GetService("DataStoreService") --> data storing :sunglasses:
local dataStore = dataStoreService:GetDataStore(datastoreName) --> bans are stored here
local players = game:GetService("Players") --> players be kinda cool too tho
local subscribeSuccess,subscribeConnection = pcall(function()
return messagingService:SubscribeAsync(messagingServiceTopic,function(message)
local userToCheckFor = message.Data --> message is a table, we're getting the data sent (refer to https://developer.roblox.com/en-us/api-reference/function/MessagingService/SubscribeAsync)
local player = players:GetPlayerByUserId(userToCheckFor)
if player then
player:Kick(kickMessage)
end
end)
end)
if not subscribeSuccess then
warn("[Error while connecting to messaging service]:",subscribeConnection)
end
-- TO BAN PEOPLE, RUN THE FUNCTION BELOW WITH THEIR USER ID
-- EX: ban(players.Player123.UserId) or ban(12345)
local ban = function(id)
local banSuccess,response = pcall(function() --> pcall basically makes sure if it errors, it doesn't break the whole code
dataStore:SetAsync(id,true)
end)
if banSuccess then
local publishSuccess,response = pcall(function()
messagingService:PublishAsync(messagingServiceTopic,id) --> This posts a message to all the other servers
end)
if not publishSuccess and response then
warn("[Error whilst publishing ban to servers]:",response) --> Sends a warning in the console that something went wrong
return false
end
else
warn("[Error while saving ban data]:",response) --> Sends a warning in the console that something went wrong
end
return banSuccess
end
-- TO UNBAN PEOPLE, RUN THE FUNCTION BELOW WITH THEIR USER ID
-- EX: unban(players.Player123.UserId) or unban(12345)
local unban = function(id)
local unbanSuccess,response = pcall(function() --> pcall basically makes sure if it errors, it doesn't break the whole code
dataStore:SetAsync(id,false)
end)
return unbanSuccess
end
local run = function(player)
local success,response = pcall(function()
return dataStore:GetAsync(player.UserId)
end)
if success and response then
-- player is banned, interesting
player:Kick(kickMessage)
elseif success then
-- player isn't banned
elseif not success then
warn("[Error while checking ban status for]: ",player,";",response)
end
end
players.PlayerAdded:Connect(run) -- When a player is added, it'll run the 'run' function
-- The player could've joined before the script ran
for _,player in pairs(players:GetPlayers()) do
coroutine.wrap(run)(player) -- A coroutine is used because datastore requests yield if that makes
sense
-- This basically ensures that if this script is run late, it doesn't take like 15 seconds to check all the players and can check them all at the same time
end
-- example ban / unban
ban(1)
unban(1)
-- same as
ban(players.Roblox.UserId)
unban(players.Roblox.UserId)
Have a good night / morning! <3
I’m sorry if this comes off as rude, I’m not trying to necessarily criticize you for everything but I want to know why you did some of the things you did. But, it will probably come off as passive agressive, oh well.
What’s the point of changing the name if this is (presumably) a server script? Even if it was changing the name on the client, this could still be deleted easily by exploiters.
And why do you repeat a wait() loop until their PlayerGui is loaded? You can kick a player as soon as they join, nothing’s wrong with that.
And then, you don’t need to pcall the kick because I’m pretty sure it’s impossible for that function specifically to error.
The code i didn’t test it and i wrote from memory and was for a old experience (like 1 - 3 months ago)
Actually i use a good code what its simillar to your code what use datastore
The repeat wait until PlayerGui is for checking was for like inserting a Ban Gui like
“lol you are banned” or something else
Thanks for notice my errors.
(im ultra bad at english grammar i just search that word)