I’ve been working on a Timed Ban for my banning module BanIt - Simple ban module for anyone, and I’m having trouble with saving data (namely a dictionary) to a DataStore.
Here’s what retrieves the data:
local succ, data2 = pcall(function()
return timedBanStore:GetAsync("TimedBans")
end)
if succ and not data2 then
data2 = {}
print("There was no data")
elseif not succ then
warn("DataStore failed. Try turning on Studio Access to API Services.")
end
Here’s my code snippet for the PlayerAdded:
Players.PlayerAdded:Connect(function(plr)
if table.find(data, plr.UserId) or table.find(serverBanTable, plr.UserId) then
plr:Kick("Banned from the game!")
elseif data2[tonumber(plr.UserId)] ~= nil then
local timeData = data2[plr]
print(typeof(timeData))
elseif data2[tonumber(plr.UserId)] == nil then
for k, v in pairs(data2) do
print(k, v)
end
print(data2[tonumber(plr.UserId)])
print("No data for user " .. plr.Name)
end
end)
These together tell me that there is a piece of data within the DataStore. However, it always takes the data2 == nil route.
Here’s what saves the data:
local function saveTimeData()
local yes, no = pcall(function()
return timedBanStore:SetAsync("TimedBans", data2)
end)
if not yes and no then
warn(no)
elseif yes then
print("BanIt | Successfully saved")
for k, v in pairs(data2) do
print(k, v)
end
elseif not yes then
print("idk")
end
end
and here’s how it’s implemented:
function BanIt.TimedBan(plrUser, num, numType)
if numType:lower() == "minutes" then
num *= 60
elseif numType:lower() == "hours" then
num *= 3600
elseif numType:lower() == "days" then
num *= 86400
end
local plr = Players:GetUserIdFromNameAsync(plrUser)
print(typeof(plr))
local current = os.time()
data2[plr] = current .. ";" .. num
print(data2[plr])
saveTimeData()
wait(1)
if Players:FindFirstChild(plrUser) then
Players[plrUser]:Kick("Banned for " .. num .. " " .. numType .. " from the game.")
end
end
Any help with this? My mind is boggled right now.