Could you explain more on how to save that value and when they join back and then determine whether its true or false.
This is what I have currently done using a table.
local SaveData = game:GetService("DataStoreService"):GetDataStore("SaveBannedData")
local banned = { }
game.Players.PlayerAdded:Connect(function(player)
while true do
wait(1)
if player does action then
table.insert(banned, player.UserId)
else
-- do nothing
end
if table.find(banned, player.UserId) then
player:kick("you are banned")
else
-- do nothing
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
SaveData:SetAsync(player.UserId, banned)
print("saved success")
end)
As I said before, this is a ban system not a black list, meaning it will ban any player that performs a certain action, its not a black list to prevent certain players from entering the game.
You would need to use GlobalDataStores to do this, and then you would approach it this way.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local BanStore = DataStoreService:GetDataStore("BanStore")
local bannedMessage = "You have been banned from this game"
function banPlayer(target)
local userId = target:IsA("Player") and target.UserId or target
local success, banned = pcall(function()
return BanStore:UpdateAsync(userId, true)
end)
if (success) then
if (target:IsA("Player")) then
target:Kick(bannedMessage)
end
end
end
function isPlayerBanned(userId)
local success, result = pcall(function()
return BanStore:GetAsync(userId)
end)
if (success and result) then
return true
end
return false
end
function PlayerAdded(player)
if (isPlayerBanned(player.UserId)) then
player:Kick(bannedMessage)
end
end
for _, player in pairs(Players:GetPlayers()) do
coroutine.wrap(PlayerAdded)(player)
end
Players.PlayerAdded:Connect(PlayerAdded)
Insside of the ban function you could add another argument named “reason” and then use :SetAsync() but instead of (true) you could do ({ true, reason })
GlobalDataStores can be accessed on all servers, so when the data store updates and the player is in your game it would kick the player.
The function I wrote for you allows you to use either a player Instance or a user id when using the ban command
sorry, This is all very confusing for me, to keep it simple I’ll give a different scenario, lets say for example if the player touches a part it should ban them, (not kick) meaning somehow we save there data (tables? data stores?) and they get kicked every time they try re-join. how would I do that?
local PlayerDataTable = {
isBanned = false, -- Make sure to have this
Inventory = {},
-- Etc
}
Now if it is false that means the player is not banned, and if it is true then the player is banned.
So whenever a player Joins check there data to see if isBanned is true.
game:GetService("Players").PlayerAdded:Connect(function(player)
-- any method to get the playersdata | DataStore2, ProfileService or your own
-- Once you have their data
if data.isBanned then player:Kick("You are banned") end -- If its true kick the player everytime they connect.
end)
local Bans = {"Username", 0000000} -- add a username (string) or a userid (number)
function checkBan(player)
for _,banned in pairs(Bans) do
if type(banned) == "string" and player.Name == banned then
return true
elseif type(banned) == "number" and player.UserId == banned then
return true
end
end
return false
end
game:GetService("Players").PlayerAdded:Connect(function(player)
if checkBan(player) == true then
player:Kick("Banned")
end
end)
As I said, your post doesnt help, because I’m not making a black list, I’m making a ban system.
I don’t want to exclude certain players, i want to make a system that will ban any player that does a certain action and then saving that ban data.