I made a script that adds the players UserId to a table if that player does a certain action, and any player within that table will be kicked from the game, and then the player UserId should save to the table, so next time the player re-joins, he will still be in that table, thus being kicked from the game, (aka a ban), Iām having trouble with making my table save.
I have tried data stores, but as Iām fairly new to scripting Iām still not sure what to do.
Any help to how exactly I can make my table save would be much appreciated.
Well, if this is a black list then it would make more sense, but if itās not, then you can set up a ban system that works with BoolValues. This is really only useful for things like custom admin.
Basically, thereās going to be a BoolValue thatās called āIsBannedā, and if someone has a ban action done to them, then the āIsBannedā value will be equal to true, with Datastores you can make sure it saves, and when the person joins again, you can use a PlayerAdded Event to check whether the value is true or false in their player. It seems like youāre new, so I donāt think I would like to give any code currently, it wouldnāt be helpful to you or me. I just simply gave the design/logic behind it.
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)