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

How do I make the if statement not end the script when the if statement is false?

Asked by 5 years ago
Edited 5 years ago

I want to make a script where if the value is a certain number the game print something but it dosen't. To fix the problem I used elseif but it still didn't work. how do I make it work?

local player = script.Parent.Parent.Parent

local char = player.Name

script.Parent.Activated:Connect(function()

if script.Parent.Value.Value == 1 then

print("k")

if script.Parent.Value.Value == 2 then

print("1")

if script.Parent.Value.Value == 3 then

print("s")

end

end

end

end)

how do I make it work for every number unless it isn't in the script? Thank you if you answer.

0
is it not printing k? starmaq 1290 — 5y
0
It prints K when the value is 1 but when it changes it dosen't print anything turquoise_monkeyman 32 — 5y
0
Use spawn() or coroutines to run the function in a separate thread. DeceptiveCaster 3761 — 5y
0
How do I use it and where do I use it? turquoise_monkeyman 32 — 5y

2 answers

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

Your script isn't working because you put each subsequent if statement inside the previous one.

if script.Parent.Value.Value == 1 then
    -- Everything between this "if" and this "end" runs ONLY IF the expression "script.Parent.Value.Value == 1" is true
    if script.Parent.Value.Value == 2 then -- The code here will never run if the value is not 1 because it's inside of the first if statement
    end
    -- This is why indentation is important; it makes it obvious what block your pieces of code are in
end

This is what you want instead:

if script.Parent.Value.Value == 1 then
    print('k')
elseif script.Parent.Value.Value == 2 then
    print('l')
elseif script.Parent.Value.Value == 3 then
    print('s')
end -- Notice that there's only one "end" in this example; that's because the if statement is compound instead of nested (look up nested loops/functions for a clearer understanding of the concept)

A general example of nested code blocks:

for i = 1,10 do -- Outer loop
    for j = i,10 do -- Inner loop
        print(i + j)
    end
    -- j is not defined out here; it is only defined INSIDE the inner loop
end
0
Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! turquoise_monkeyman 32 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

So basically:

    value = 5

if value == 5 then
print('value = 5 (true)')
else
print('value does not equal 5. (false)')
end

If the value is 5 it'll print 'value = 5 (true)' if it isn't it will print 'value does not equal 5. (false)'

0
I want it to work for 3 or more values turquoise_monkeyman 32 — 5y
0
pcall() DeceptiveCaster 3761 — 5y
0
whats that? turquoise_monkeyman 32 — 5y

Answer this question