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

"wait" does not work when "or" is used? (this isn't specific enough, title bot?)

Asked by 6 years ago

This script is supposed to make a GUI visible when someone approaches the intersection of US-201 and ME-3. It does. The script is also supposed to make the GUI disappear after 5 seconds. That's where the problem lies: it doesn't do that. The GUI just stays on the person's screen for eternity. I think the "or" in the if statements is causing it. I have an identical script that works perfectly fine that doesn't have "or" anywhere. Here's the script without "or"

local Part = game.Workspace:WaitForChild("BelfastVoltisonGPSSignal")
local Gui = game.Players.LocalPlayer.PlayerGui.BelfastVoltisonGPSSignalScreenGui.Frame

Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and game.Players.LocalPlayer.JobDestination.Value ~= "b" then
        Gui.Visible = true
        if game.Players.LocalPlayer.JobDestination.Value == "Augusta ME" then
            Gui.TextLabel.Text = "Turn left onto Maine State Route 3"
        wait(5)
        Gui.Visible = false
                end
        end
    end)

Here's the broken script that includes "or"

local Part = game.Workspace:WaitForChild("ME-3/US-201/ME-100GPSSignal")
local Gui = game.Players.LocalPlayer.PlayerGui.ME3US201GPSSignalScreenGui.Frame

Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and game.Players.LocalPlayer.JobDestination.Value ~= "b" or "Belfast ME" then
        Gui.Visible = true
        if game.Players.LocalPlayer.JobDestination.Value == "Augusta ME" or "Brunswick ME" or "Rockland ME" then
            Gui.TextLabel.Text = "Turn left onto ME-100/US-201" 
        else game.Players.LocalPlayer.JobDestination.Value = "Portland ME" or "Rumford ME" or "Farmington ME" or "Lewiston ME" or "Kingfield ME" or "Fryeburg ME" or "Biddeford ME" or "Sanford ME"
            Gui.TextLabel.Text = "Continue straight to stay on ME-3"
        wait(5)
        Gui.Visible = false
                end
        end
    end)
1
When you use "or", it makes a completely separate statement. Because of that, you need to re-specify the value you are comparing. E.g. `if a == b or a == c then` is correct, `if a == b or c then` is not actually comparing a to c. It's just seeing if c is not nil in the second instance. EzraNehemiah_TF2 3552 — 6y
0
^not nil or false User#19524 175 — 6y
0
lol (this isn't specific enough, title bot?) namespace25 594 — 6y
0
Yeah but I don't know man, sorry ): namespace25 594 — 6y

1 answer

Log in to vote
3
Answered by 6 years ago

Problem

The problem is on lines 6, 7 and 9, but I'll use 7.

if game.Players.LocalPlayer.JobDestination.Value == "Augusta ME" or "Brunswick ME" or "Rockland ME" then

You're using or incorrectly. x or y evaluates to x if x is truthy, y otherwise.

Truthy values

>print(true or false)
true

>print(nil or 9)
9

Truthy values are anything that is not false or nil in Lua. In the first example, true is printed. false isn't even checked, as the result was going to be truthy. In the second example, nil is checked first, but it's falsey, so it checks 9, and 9 is printed since it's a number, and numbers are not false or nil, so they are treated as true in a boolean context. A string is truthy as well, and therefore your if will always execute, even if the first condition is not met.

Solution

This would be what you're looking for:

local job = game.Players.LocalPlayer.JobDestination -- Variables! Use them 

if job.Value == "Augusta ME" or job.Value == "Brunswick ME" or job.Value == "Rockland ME" then
    --...
end

However that can get tedious very quickly especially since you have a lot of job destinations. So I suggest you use a table, specifically a dictionary, and store the job destinations in there.

local job = game.Players.LocalPlayer.JobDestination
local destinations = {
    ["Augusta ME"] = true;
    ["Brunswick ME"] = true;
    -- And so on. Remember to separate each entry with semicolons or commas! 
}

if destinations[job.Value] then -- Check if the job value is in the dictionary.
    -- Do stuff. 
end
1
Sorry, I totally forgot about this topic! Anyway, thank you for saving me. Enjoy some reputation. sesamert16 31 — 6y
Ad

Answer this question