So I’m making a ban system within my game using Roblox’s BanAPI and want to let the admins of the game ban and unban players with the admin panel inside the game.
The problem is, seem like Roblox doesn’t have a method to check if a player is being ban or not, so I have to make my own detection system using BanHistoryPages and DateTime.
Though, I’m unsure that whether StartTime in BanHistoryPages returns Universal Time or Local Time.
I have searched from multiple sources but couldn’t found the one I want, so helps are appreciated!
It would most likely be stored in universal time. You would use DateTime.fromIsoDate to reconstruct the DateTime object for that timestamp. You do not need to use any date-time logic to determine if a player is banned, however. Within the BanHistoryPages object, there exists a field called “Ban”, which is a boolean that represents whether or not the player was banned or un-banned at that moment in time. You need only pull the latest ban record for the given player and read off that field:
local function isBannedAsync(userId: number): boolean
if RunService:IsStudio() then
warn("Cannot determine ban status while in non-production server.")
return
end
local banHistory = Players:GetBanHistoryAsync(userId)
local latestHistory = banHistory:GetCurrentPage()[1]
if latestHistory then
return latestHistory.Ban
end
return false
end
Thanks for your reply!
Though there’s a few thing I want to inform you.
The key “Ban” in BanHistoryPages will show “true” even if the ban expired and only shows “false” when the player is unbanned by :UnbanAsync()
Here’s an image showing that my 1 minute ban is expired:
Here’s an image showing my ban history: (ban no.3)
The reason why page 2 said “false” is because I unbanned myself via Roblox’s website.
Anyways, thanks and have a nice day!
Ah. You’re right. I did not consider that. Here is a revised function:
local function isBannedAsync(userId: number): boolean
if RunService:IsStudio() then
warn("Cannot determine ban status while in non-production server.")
return
end
local banHistory = Players:GetBanHistoryAsync(userId)
local latestHistory = banHistory:GetCurrentPage()[1]
if not latestHistory then
return false
end
if not latestHistory.Ban then
return false
end
if latestHistory.Duration < 0 then
return true
end
local startTime = DateTime.fromIsoDate(latestHistory.StartTime)
local currentTime = DateTime.now()
return currentTime.UnixTimestamp - startTime.UnixTimestamp < latestHistory.Duration
end