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

Why doesn't this If statement run?

Asked by 9 years ago

Am I using Or wrong? The map values change when a player clicks the GUI, I've tested and looked over the numbers, when it's 4 or 7 it wont turn indivisible. I don't get it...

--This one works:
while wait() do
if workspace.Map.Value ~= 6 then
    script.Parent.Visible = true
elseif workspace.Map.Value == 6 then
    workspace.Type.Value = 0
    script.Parent.Visible = false
end


--And yet this one doesn't work:
while wait() do
if workspace.Map.Value ~= 4 or workspace.Map.Value ~= 7 then--Why doesn't this work?
    script.Parent.Visible = true
elseif workspace.Map.Value == 4 or workspace.Map.Value == 7 then
    workspace.Type.Value = 0
    script.Parent.Visible = false
end

EDIT: Here's a perfect example http://wiki.roblox.com/index.php?title=Or_operator#Or Right below the Or operator it gives this example "if soul == true or food == true or try == true then" So how come this works? But yet I cant do the same with numbers?

0
I don't understand what you are trying to do. fahmisack123 385 — 9y
0
I dont get why lines 13 and 14 dont work when line 3 and 5 work. When I remove the Or, it works fine with 4, but I need 7 there as well. Orlando777 315 — 9y

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

This is a logical error.

Consider this conditional more closely:

workspace.Map.Value ~= 4 or workspace.Map.Value ~= 7

If the Value is '4', the second half of that is true and thus the whole thing is true. If the Value is '7', the first half is true and the whole thing is true. Any other number, any both halves are true.

To fix this, change that or to an and, since you only want that if to be executed when the Value is neither '4' nor '7'.

Alternatively, I would probably write this code like this:

while wait() do
if workspace.Map.Value == 4 or workspace.Map.Value == 7 then
    script.Parent.Visible = false
    workspace.Type.Value = 0
else --not 4 or 7
    script.Parent.Visible = true
end
0
Ugh, total brainfart. Thank you +1ed and accepted answer. Orlando777 315 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

I don't know how both loops are working because you're missing an end for each one. Did an error not show up in the output or something?

while wait() do
    if workspace.Map.Value ~= 6 then
        script.Parent.Visible = true
    elseif workspace.Map.Value == 6 then
        workspace.Type.Value = 0
        script.Parent.Visible = false
    end
end

while wait() do
    if workspace.Map.Value ~= 4 or workspace.Map.Value ~= 7 then
        script.Parent.Visible = true
    elseif workspace.Map.Value == 4 or workspace.Map.Value == 7 then
        workspace.Type.Value = 0
        script.Parent.Visible = false
    end
end
0
I'm not missing an End, you only need a End if you're using Else not Elseif Orlando777 315 — 9y
0
Read this, you do need an end when you use a while loop. http://wiki.roblox.com/index.php?title=In-Depth_Scripting_Guide#The_.27end.27_Statement Spongocardo 1991 — 9y
0
I do, they are even lines up, first end is for the If statement, which the elseif is in, the second end is for the loop. Orlando777 315 — 9y
0
I only say one end for each loop in your code, so I wouldn't have known. Spongocardo 1991 — 9y

Answer this question