Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

4 comments

0

is it not printing k?

0

It prints K when the value is 1 but when it changes it dosen't print anything

0

Use spawn() or coroutines to run the function in a separate thread.

0

How do I use it and where do I use it?

2 answers

✓ Accepted answer

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

1 comment

0

Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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

3 comments

0

I want it to work for 3 or more values

0

pcall()

0

whats that?

This discussion is preserved as it was. New answers, comments, and votes are closed.