Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does "not" get nil values or false values and does "if" get existed value or true values?

Asked by 5 years ago
Edited 5 years ago

I was using not, and not seems to activate when something is nil and false. Same goes for if, but values that exist and is true. Here's the code I'm trying to make, but it messes it up.

-- ban script
local bans = game:GetService("DataStoreService"):GetDataStore("Bans")
game:GetService("Players").PlayerAdded:Connect(function(plr)
    -- trying to use it for nil
    if not bans:GetAsync("player_" .. plr.userId) then
        -- but it fires false and nil
        -- how do i know
        -- datastore throws an throttle error
        -- and debug
        print(bans:GetAsync("player_".. plr.userId)) -- false or nil
        bans:SetAsync("player_" .. plr.userId, false)
    end
    if bans:GetAsync("player_" .. plr.userId) then -- still kicks if its false
        print(bans:GetAsync("player_".. plr.userId)) -- false or true
        plr:Kick()
    end
end)
0
say if bans:GetAsync("player_"..plr.UserId) then instead of line 5 greatneil80 2647 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

In Lua, all values are considered "true" by "if" except false and nil. "not" inverts it (so that something that is considered true becomes false and vice versa). Thus, your first couple sentences are correct.

Datastores have limits, so call GetAsync and SetAsync as few times as possible (and there's no reason to call GetAsync more than once here). Also, a general tip: it is better to combine lots of data into a single key rather than call GetAsync several times (accessing only a single value each time). (If you only need a few values and aren't good with manipulating tables, it shouldn't be a problem to keep it simple.)

In your case, you really only need to store "true" if a player has been banned and you can leave it as "nil" otherwise. This eliminates the need for lines 4-12.

I'm not quite sure how line 14 could print "false", though GetAsync does cache values for about 4 seconds (which might be partially responsible). Calling GetAsync just once should fix the problem; if not, there might be something else going on in your scripts. If not, then maybe a Roblox bug is involved; I would try assigning the result of GetAsync to a local variable and using that in the 'if' statement.

0
A general example I could use for not is a nil or false value in a table, i.e. local t = {a = false, b = nil} and if I call not, for example, not t[key], it would do the if statement if false and nil. And it's getting/becoming a problem for me. So do I have to do == false or == nil? User#24541 10 — 5y
0
(Note that you cannot store 'nil' values in tables: all values start out as 'nil' and assigning 'nil' to a key actually removes it from the table.) As for your question, that's up to what you want "false" and "nil" to mean, but I recommend treating them as the same thing. In your code, then, you would never "== false" or "== nil", you'd just use "if not". chess123mate 5873 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

On line 5 and 13 it basically checks if it exists. It'd be the same if I would say:" If you see an apple, eat it". However, you need to compare the value of the object. For example:"If you see an apple and it is red, eat it". Apple is the object, red is the value.

So, to fix, just add == true or == false. This won't work if you did not, store it as boolean values.

Answer this question