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

Why this if clause only work for the first sentences??

i have this variable

local Undeletable = "Golden Dominus" or "Aqua Dragon" or "Dominus Aura" or "Shiny Dog" or "Shiny Cat" or "Shiny Chicken" or "Shiny Turtle"

and this is the if clause

v.MouseButton1Click:Connect(function()
	if PetNames == Undeletable then
		delete.Visible = false
	else
		delete.Visible = true
	end
				
	if PetNames == Undel then
		shiny.Visible = false
	else
		shiny.Visible = true
	end
end)

so, when the PetNames is Golden Dominus it will work. the delete and the shiny wont show up, but when the petnames is not golden dominus why the delete and the shiny is showing??? do i need to type every each one of the pet or is there any easiest method because there will be like more than 10 undeletable pets :/

1 answer

✓ Accepted answer

This don't works because of coding logic(don't worry, i'll explain), so, basically, that variable definition is just a logic for code, waiting for the first true:

If you do

local var = "Text" or "Other text"

var will aways be "Text"

thats because of logic, basically, your code is trying to get a true on that, and because its a string, its aways true.


local var = (var1 == 1) and "text" or "other text"

^^ this is also coding logic, basically, it will set the variable to "text" if var1 is equals to 1, if its not, it will set to "other text"

I know this seems complicated, but its not easy to explain it.


Now into your code, that variable Undeletable is aways "Golden Dominus", what you can do is defining that strings inside a table, and then checking if equals to any of the values.

Here's how to do it:

local Undeletable = { -- Start declaration of table
"Golden Dominus", "Aqua Dragon", "Dominus Aura", "Shiny Dog", "Shiny Cat", "Shiny Chicken", "Shiny Turtle"
} -- end declaration of table (( the table can be all on the same line ))

Now, your variable is a table, so, how to check the values?

We can use a for ... do loop:

v.MouseButton1Click:Connect(function()
    local cantdelete = false -- here, we create a variable on the MouseButton1Click scope
    for _,v in pairs(Undeletable) do
        if PetNames == v then
            cantdelete = true  -- if the object can't be deleted, we change that variable to true
            break -- and break the loop, so it wont run any more, as its not needed
        end
    end
    -- now we can simply check if that variable we defined is true or false
    if cantdelete then -- if object cant be deleted
        delete.Visible = false
    else
        delete.Visible = true
    end

    if PetNames == Undel then -- variable undel isn't defined
        shiny.Visible = false
    else
        shiny.Visible = true
    end
end)

Useful links:

DevAPI/Scopes

DevAPI/Loops#for-do

DevAPI/Tables

DevAPI/Conditional Structures


if this helps, please upvote and mark this answer as accepted

8 comments

0

:|

0

?

0

this looks a bit complicated

0

ohhh just noticied your answer, looks like I was faster

0

its not, the complicated part is the coding logic, there's nothing exactally explaining it anywhere on the internet

0

yes and i think mine is the right one

0

i would just do and instead of the table

0

Thanks! :D

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