DataStores!
The easiest way to handle this is by using a DataStore
which would be able to save the Player’s Ban, in your instance we’re wanting to “ban” a Player when we chat a specific command
We’d need to first get all of the Players that are currently in the server, then detect if that specific Player is equal to what we want to search for here:
Player.Chatted:Connect(function(Msg)
for _, Target in pairs(game.Players:GetPlayers()) do
if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid
end
end
end)
The next thing, is that there are 2 functions that should hopefully help us with this Ban Command:
-
SetAsync
, which will save that specific Player’s Ban (With a provided key)
-
GetAsync
, which would load that Player’s Ban if they have one (Given a provided key)
What we could possibly do then, is upon when a player first joins the game, is go ahead & check if there’s a valid “Ban key” for the player that joined
local DataService = game:GetService("DataStoreService")
local DataName = DataService:GetDataStore("Bans")
game.Players.PlayerAdded:Connect(function(Player)
local BanCheck
local success, whoops = pcall(function()
BanCheck = DataName:GetAsync(Player.UserId)
end)
if success and BanCheck then --If both results outputted as true, then we can ban this player >:(
Player:Kick("You have been banned from this game, reason: MEANIE")
end
end)
We’d always want to encase our DataStore functions in pcalls
so that they won’t error & break the script, this would return back as a “warning” or a “success” if what we’re checking for is valid or not
Back to our looping script, now this is where we’d want to call SetAsync()
to save the Player’s Ban ID to prevent them from rejoining
Player.Chatted:Connect(function(Msg)
for _, Target in pairs(game.Players:GetPlayers()) do
if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid
local success, whoops = pcall(function()
DataName:SetAsync(Target.UserId)
end)
if success then
print(Target.Name.." has been added to the ban list!")
else
warn("An error occurred while trying to save this Player's Ban: ", whoops)
end
Player:Kick("You have been banned from this game, reason: You put pineapple on pizza")
end
end
end)
SetAsync
would set that specific Player’s UserId as a key, and that once they rejoin they’ll be banned
Hopefully, our full code should look like this!
local DataService = game:GetService("DataStoreService")
local DataName = DataService:GetDataStore("Bans")
game.Players.PlayerAdded:Connect(function(Player)
local BanCheck
local success, whoops = pcall(function()
BanCheck = DataName:GetAsync(Player.UserId)
end)
if success and BanCheck then --If both results outputted as true, then we can ban this player >:(
Player:Kick("You have been banned from this game, reason: MEANIE")
end
Player.Chatted:Connect(function(Msg)
for _, Target in pairs(game.Players:GetPlayers()) do
if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid
local success, whoops = pcall(function()
DataName:SetAsync(Target.UserId)
end)
if success then
print(Target.Name.." has been added to the ban list!")
else
warn("An error occurred while trying to save this Player's Ban: ", whoops)
end
Player:Kick("You have been banned from this game, reason: You put pineapple on pizza")
end
end
end)
end)