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

whats wrong with my if statements and variables?

I'm not sure what is wrong. this is my first time doing scripting in lua but i know python. The code works except the if statements and i think there is a problem when i try to change "switch" between true and false. The code changes the part "load" to either transparent and non collide or solid and collide depending on what switch is.

here is the code:

switch = true
if swtich = true
	function onClicked()
	script.Parent.Parent.load.Transparency = 1
	script.Parent.Parent.load.CanCollide = false
	swtich = false
	end
	script.Parent.ClickDetector.MouseClick:connect (onClicked)
	

if swtich = false then	
	function onClicked()
	script.Parent.Parent.load.Transparency = 0
	script.Parent.Parent.load.CanCollide = true
	swtich = true
	end
	script.Parent.ClickDetector.MouseClick:connect (onClicked)
	

2 answers

✓ Accepted answer

Alright, so I've helped you place your code into a code block, so now let's go over a couple things:

  1. you spelled switch wrong on line 11 and line 2
  1. you need end's as well as then

Here's an example of a conditional statement in use:

local banana = true

if banana == true then
	print("Hello")
end -- this is what you're missing

If placed into any script it will print:

Hello

So, how do you fix the script? Well we can start by creating proper indention and making sure we have the correct amount of end's

local switch = true -- local variables are preferred in this case

if switch then-- this is similar to if switch == true then

end

if not switch then -- this is similar to if switch == false then

end

Next, let's have the function outside so that it's we don't need that many:

local switch = true -- local variables are preferred in this case

local function onClicked()
	if switch then
		script.Parent.Parent.load.Transparency = 1
		script.Parent.Parent.load.CanCollide = false
		switch = false
	elseif not switch then
		script.Parent.Parent.load.Transparency = 0
		script.Parent.Parent.load.CanCollide = true
		switch = true
	end
end

if switch then-- this is similar to if switch == true then
	script.Parent.ClickDetector.MouseClick:Connect(onClicked)
end

if not switch then -- this is similar to if switch == false then
	script.Parent.ClickDetector.MouseClick:connect (onClicked)
end

Now you might notice that we did the condition twice which is unnecessary so:

local switch = true -- local variables are preferred in this case

local function onClicked()
	if switch then
		script.Parent.Parent.load.Transparency = 1
		script.Parent.Parent.load.CanCollide = false
		switch = false
	elseif not switch then
		script.Parent.Parent.load.Transparency = 0
		script.Parent.Parent.load.CanCollide = true
		switch = true
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

This is all we need!

Hope this helped! Best of luck!

When assigning a value to a variable you use one =, when checking the value of a variable you do ==. This is what I think is causing your code to error

You also spelt switch wrong at each if statement.

for example this is incorrect

var = 1

if var = 1 then
 print("hello')
end

however, this is correct.

var = 1

if var == 1 then
 print("hello')
end

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