Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How come my datastore stuff is coming back nil?

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

2 comments

0

You're wasting the function do this instead "return PlayerStats:GetAsync(player.Name.." BanStats")" : line 3

0

You should store your data store keys as their UserIds instead because if a player changed their name they would be unbanned

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.