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

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

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)

1 comment

0

say if bans:GetAsync("player_"..plr.UserId) then instead of line 5

2 answers

✓ Accepted answer

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.

2 comments

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?

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".

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.

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