:SetAsync(Key,Data) Data is for you a table. so it woudent store the Reason. just the banstart time and bantime and i dont think you need to store the ban start time. just Bantime = os.time() + bantimeinseconds
Looking through your code, it doesn’t seem like you are using the DataStore functions correctly. Is there any output related to this script that could help find the problem?
SetAsync() only accepts two parameters, that is the store key and the value.
RemoveAsync() only accepts one parameter, that is the store key.
For consistency, you should also consider using tostring on all of the places you are using a userId for the DataStore key.
What does your code look like to checking if someone is banned as well?
Pretty sure you have to remove and add them 1 at a time. Also if you’re confused of how to remove the reason, and bantime start from the beginning where you banned the player and add 2 other values inside of the playerkey which is the player’s id. I don’t know if this would work, but you could give it a try.
_G.UnBan = function(userId)
local data = game:GetService("DataStoreService"):GetDataStore("BanData")
data:RemoveAsync(userId[reason])
data:RemoveAsync(userId[bantime])
data:RemoveAsync(userId)
end
game.Players.PlayerAdded:Connect(function(plr)
if plr.UserId == game.CreatorId then return end
local Success, Result = pcall(function() -- It is good practice to wrap your datastore requests in pcalls
return tempData:GetAsync(tostring(plr.UserId), "TempBan", reason) -- Returns the players temp ban data for later use.
end)
if Success then -- Checks if the DataStore request was successful
if Result then -- Checks if there is any data under the key
if Result.BanStartTime + Result.bantime > os.time() then -- This is the part that checks if the player is temp banned or not
local status = tempData:GetAsync(tostring(plr.UserId), "TempBan", reason)
local t = os.date('!*t',Result.BanStartTime + Result.bantime)
if Result.bantime > 999999 then
plr:Kick("You are banned until: ETERNITY")
return
end
local month = t.month
local day = t.day
local year = t.year
if status then
plr:Kick("You are banned until: "..month.."/"..day.."/"..year)
end
else
print('Thing')
end
end
end
end)
I’d try simply using SetAsync instead of RemoveAsync. There’s not much of a point to using RemoveAsync when SetAsync allows for much more flexibility in what replaces it.
Also, I wouldn’t use _G declarations for ban/unban methods.