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