I have been stuck on how to make this work for months now (mostly because I haven't been using Roblox Studio for a while. I don't know if I'm doing something wrong in terms of getting the datastore. I attempt to GetAsync inside a pcall, but every time I do so it always returns nil. Is there some way to stop this?
The error being thrown:
15:51:40.445 Workspace.BlacklistHandler:115: attempt to index nil with 'BanTime' - Server - BlacklistHandler:115
local bantable=nil local success, err = pcall(function() bantable=PlayerStats:GetAsync(player.Name.." BanStats") end) if success and bantable ~= nil then BanTime.Value = bantable.BanTime BanReason.Value = bantable.BanReason BanSender.Value = bantable.BanSender else BanTime.Value = 0 BanReason.Value = nil BanSender.Value = nil end wait(1) if success then if os.difftime(os.time(),bantable.BanTime)<bantable.BanTime then -- and player.UserId~=29320767 if bantable.BanTime==math.huge then player:Kick("You're permabanned! Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>86400*7 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/86400*7)).." weeks(s). Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>86400 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/86400)).." days(s). Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>3600 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/3600)).." hour(s). Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>60 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/60)).." minutes(s). Reason:"..bantable.BanReason) end end else error(err) end
Read the comments on your question too.
Problem
This happens because if the player isn't banned(line 12) then the function will keep on running causing the "attempt to index nil" error because "bantable" is nil so basically your code looks like this at run time(if the player isn't banned):
if os.difftime(os.time(),nil.BanTime)<nil.BanTime then
Solution
So we can use the keyword "return" to stop the function though that isn't the purpose of the keyword "return".
Code
local success, bantable = pcall(function() return PlayerStats:GetAsync(player.Name.." BanStats") end) if success and bantable ~= nil then BanTime.Value = bantable.BanTime BanReason.Value = bantable.BanReason BanSender.Value = bantable.BanSender elseif success and bantable == nil then BanTime.Value = 0 BanReason.Value = nil BanSender.Value = nil return end wait(1) if success then if os.difftime(os.time(),bantable.BanTime)<bantable.BanTime then -- and player.UserId~=29320767 if bantable.BanTime==math.huge then player:Kick("You're permabanned! Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>86400*7 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/86400*7)).." weeks(s). Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>86400 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/86400)).." days(s). Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>3600 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/3600)).." hour(s). Reason:"..bantable.BanReason) elseif os.difftime(os.time(),bantable.BanTime)>60 then player:Kick("You're still banned for ".. math.ceil(((os.difftime(os.time(),bantable.BanTime))/60)).." minutes(s). Reason:"..bantable.BanReason) end end else error(bantable) end