You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make a ban and unban system.
What is the issue? Include screenshots / videos if possible!
it kicks me from the server when i ban myself and kicks for the reason also but when i rejoin it doesn’t kicks me. ( No Errors and I Am Trying On Roblox Studio And I Don’t Think That’s The Problem )
local admins = game:GetService('ServerStorage').Admins
local DSS = game:GetService('DataStoreService')
local playerData = DSS:GetDataStore('playerData')
local function setBan(player, reason)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
end)
if not success then
warn(errorMessage)
else
game:GetService('Players'):FindFirstChild(player):Kick(reason)
end
end
local function setUnBan(player)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = false})
end)
if not success then
warn(errorMessage)
end
end
local function getBan(player)
local data
local sucess, errorMessage = pcall(function()
data = playerData:GetAsync("bans/" .. tostring(player.UserId))
end)
if sucess and data then
if data.Ban == true then
return true, data.BanReason
else
return false
end
end
end
game:GetService('ReplicatedStorage').RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
if admins:FindFirstChild(player.UserId) ~= nil then
if string.lower(command) == 'ban' then
if game:GetService('Players'):FindFirstChild(plr) then
setBan(plr, reason)
end
elseif string.lower(command) == 'unban' then
setUnBan(plr)
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
local banned = getBan(player)
if banned == true then
player:Kick(banned[1])
end
end)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I Tried Looking On If Even It Saves On DataStore In Data Editor v2 ( Free Version ) but it just shows nil ( default value ).
I’m fairly sure you’re indexing banned = getBan wrong. You should only be returning the BanReason.
Code:
--//Services
local Players = game:GetService("Players")
local DSS = game:GetService('DataStoreService')
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
--//Variables
local admins = ServerStorage.Admins
local playerData = DSS:GetDataStore('playerData')
--//Functions
local function setBan(player, reason)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
end)
if not success then
warn(errorMessage)
else
Players:FindFirstChild(player):Kick(reason)
end
end
local function removeBan(player)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = false})
end)
if not success then
warn(errorMessage)
end
end
local function getBan(player)
local data = nil
local sucess, errorMessage = pcall(function()
data = playerData:GetAsync("bans/" .. tostring(player.UserId))
end)
if not sucess then
warn(errorMessage)
return "Unable to load ban data"
end
if data and data.Ban then
return data.BanReason
else
return false
end
end
ReplicatedStorage.RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
if admins:FindFirstChild(player.UserId) then
if string.lower(command) == 'ban' then
if Players:FindFirstChild(plr) then
setBan(plr, reason)
end
elseif string.lower(command) == 'unban' then
removeBan(plr)
end
end
end)
Players.PlayerAdded:Connect(function(player)
local banReason = getBan(player)
if banReason then
player:Kick(banReason)
end
end)
Also, I’m not sure what value plr is. Is that a name or an actual player? If you could show your client code that would help too.
Did my script work? If not, switch the server script to this:
--//Services
local Players = game:GetService("Players")
local DSS = game:GetService('DataStoreService')
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
--//Variables
local admins = ServerStorage.Admins
local playerData = DSS:GetDataStore('playerData')
--//Functions
local function setBan(player, reason)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
end)
if not success then
warn(errorMessage)
else
Players:FindFirstChild(player):Kick(reason)
end
end
local function removeBan(playerName)
local userId = Players:GetUserIdFromNameAsync(playerName)
if not userId then
return
end
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. userId, {Ban = false})
end)
if not success then
warn(errorMessage)
end
end
local function getBan(player)
local data = nil
local sucess, errorMessage = pcall(function()
data = playerData:GetAsync("bans/" .. tostring(player.UserId))
end)
if not sucess then
warn(errorMessage)
return "Unable to load ban data"
end
if data and data.Ban then
return data.BanReason
else
return false
end
end
ReplicatedStorage.RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, playerName, reason)
if admins:FindFirstChild(player.UserId) then
if string.lower(command) == 'ban' then
local commandVictim = Players:FindFirstChild(playerName)
if commandVictim then
setBan(commandVictim, reason)
end
elseif string.lower(command) == 'unban' then
removeBan(playerName)
end
end
end)
Players.PlayerAdded:Connect(function(player)
local banReason = getBan(player)
if banReason then
player:Kick(banReason)
end
end)
I modified a little bit your code and still doesn’t works.
local admins = game:GetService('ServerStorage').Admins
local DSS = game:GetService('DataStoreService')
local playerData = DSS:GetDataStore('playerData')
local function setBan(player, reason)
local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(UserId), {Ban = true, BanReason = reason})
end)
if not success then
warn(errorMessage)
else
game:GetService('Players'):FindFirstChild(player):Kick(reason)
end
end
local function setUnBan(player)
local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(UserId), {Ban = false})
end)
if not success then
warn(errorMessage)
end
end
local function getBan(player)
local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
local data
local sucess, errorMessage = pcall(function()
data = playerData:GetAsync("bans/" .. tostring(UserId))
end)
if data and data.Ban then
return data.BanReason
else
return false
end
end
game:GetService('ReplicatedStorage').RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
if admins:FindFirstChild(player.UserId) ~= nil then
if string.lower(command) == 'ban' then
if game:GetService('Players'):FindFirstChild(plr) then
setBan(plr, reason)
end
elseif string.lower(command) == 'unban' then
setUnBan(plr)
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
local banned = getBan(player)
if banned then
game:GetService('Players'):FindFirstChild(player):Kick(banned)
end
end)
It saves to the datastore because it printed (‘datastore added to queue’) after i accidently clicked twice to ban but it doesn’t kicks or it doesn’t get the player data.
--//Services
local Players = game:GetService("Players")
local DSS = game:GetService('DataStoreService')
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
--//Variables
local admins = ServerStorage.Admins
local playerData = DSS:GetDataStore('playerData')
--//Functions
local function setBan(player, reason)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
end)
if not success then
warn(errorMessage)
else
player:Kick(reason)
end
end
local function removeBan(playerName)
local userId = Players:GetUserIdFromNameAsync(playerName)
if not userId then
return
end
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. userId, {Ban = false})
end)
if not success then
warn(errorMessage)
end
end
local function getBan(player)
local data = nil
local sucess, errorMessage = pcall(function()
data = playerData:GetAsync("bans/" .. tostring(player.UserId))
end)
if not sucess then
warn(errorMessage)
return "Unable to load ban data"
end
if data and data.Ban then
return data.BanReason
else
return false
end
end
ReplicatedStorage.RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, playerName, reason)
if admins:FindFirstChild(player.UserId) then
if string.lower(command) == 'ban' then
local commandVictim = Players:FindFirstChild(playerName)
if commandVictim then
setBan(commandVictim, reason)
end
elseif string.lower(command) == 'unban' then
removeBan(playerName)
end
end
end)
Players.PlayerAdded:Connect(function(player)
local banReason = getBan(player)
if banReason then
player:Kick(banReason)
end
end)
Also, please tell me if there are any errors. Also, .Text is supposed to be the player’s name right?
Yes Person.Text is Player and Reason.Text is Reason But They Works.
it didn’t work again.
local admins = game:GetService('ServerStorage').Admins
local DSS = game:GetService('DataStoreService')
local playerData = DSS:GetDataStore('playerData')
local function setBan(player, reason)
local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(UserId), {Ban = true, BanReason = reason})
end)
if not success then
warn(errorMessage)
else
game:GetService('Players'):FindFirstChild(player):Kick(reason)
end
end
local function setUnBan(player)
local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
local success, errorMessage = pcall(function()
playerData:SetAsync("bans/" .. tostring(UserId), {Ban = false})
end)
if not success then
warn(errorMessage)
end
end
local function getBan(player)
local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
local data
local sucess, errorMessage = pcall(function()
data = playerData:GetAsync("bans/" .. tostring(UserId))
end)
if not sucess then
warn(errorMessage)
return "Unable to load ban data"
end
if data and data.Ban then
return data.BanReason
else
return false
end
end
game:GetService('ReplicatedStorage').RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
if admins:FindFirstChild(player.UserId) ~= nil then
if string.lower(command) == 'ban' then
if game:GetService('Players'):FindFirstChild(plr) then
setBan(plr, reason)
end
elseif string.lower(command) == 'unban' then
setUnBan(plr)
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
local banned = getBan(player)
if banned then
game:GetService('Players'):FindFirstChild(player):Kick(banned)
end
end)