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