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

Why doesn't my "if not" statement do the opposite of a "if" statement?

Asked by
sydre2 25
5 years ago
Edited 5 years ago

I'm making a GUI where the player can select a book to use. When the player has done this, I want to see if the player has selected the same book as the player used before or if the player has selected another book so that I don't change the datastore without needing to. I do this by writing an if statement that checks if the book that the player currently has equipped is the same book that the player equipped in the GUI and stop the datastore from being changed if that is the case. However, the following code does not do that. If I change it from "if not" to "if" and then try to equip the same book as I already have equipped, The code run. but if I put back the "if not" and try to equip a book that I don't have equipped, the code doesn't run.

The code is in a server script in ServerScriptStorage

"equipped" stands for the book the player wants to equip

"writingtool" is the dictionary where I store information about the players' stats

"what" is what the player wants to equip between a pen and a book

Code:

game.ReplicatedStorage.EquipPen.OnServerEvent:Connect(function(Player, equipped, what)
    if what == "Pen" then
        writingtool = WritingTool:GetAsync(Player.UserId)
    if not writingtool["Equipped"] == equipped then
        writingtool["Equipped"] = equipped
        WritingTool:SetAsync(Player.UserId, writingtool)
    end
    else
        writingtool = WritingTool:GetAsync(Player.UserId)
        print(equipped)
        print(writingtool["Equipped2"])
        if not writingtool["Equipped2"] == equipped then
            print("Hello world!")
            writingtool = WritingTool:GetAsync(Player.UserId)
        writingtool["Equipped2"] = equipped
        WritingTool:SetAsync(Player.UserId, writingtool)
    end
        end
end)
0
Don't forget to accept my answer if it helps out User#24403 69 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Operator precedence

not has higher precedence than == so the not is applied to to writingtool["Equipped2"], making it false, and you're comparing it to something that is NOT false.

You must either wrap the writingtool["Equipped2"] == equipped in parentheses (since parentheses have highest precedence) or use the ~= operator instead

if not (writingtool["Equipped2"] == equipped) then
-- # or # --
if writingtool["Equipped2"] ~= equipped then

You should also consider wrapping data store requests in pcall since they are web requests and you cannot guarantee there won't be an error. And a debounce, since someone can spam fire the remote event, throttling the request limit.

1
ooooooh, yeah that makes sence sydre2 25 — 5y
1
Btw, somethings wrong with my pc and I connot accept your answer. I wil accept your answer later sydre2 25 — 5y
0
I accepted the answer. TheeDeathCaster 2368 — 5y
Ad

Answer this question