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

My Variable script cancels itself out. Any way to fix this?

I have a script that is supposed to toggle a variable. It doesn't work, likely because the script is cancelling itself out. How do I fix this? There's no errors, just the script doing its job and therefore stopping itself from doing its job.

function onClicked()

 if script.Parent.Parent.Steamflow == true then
  script.Parent.Parent.Steamflow = false
 end
 if script.Parent.Parent.Steamflow == false then
  script.Parent.Parent.Steamflow = true
 end

end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 answer

✓ Accepted answer

You're checking if the instance is equal to true rather than the value it holds, so it'll always equate to false. You can get the value of an instance through the Value property. Also, since you're just inverting the state, you can use not, which will turn true to false and false to true. (it does a lot more than that but I won't get into it right now)

local steamFlow = script.Parent.Parent.Steamflow
function onClicked()
	steamFlow.Value = not steamFlow.Value
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

One last thing to keep in mind is that connect is depreciated. Use Connect instead, which does the same thing.

3 comments

0

ginger boi

0

very nice answer, i like it alot

0

"not" is a thing? nice. Take my approval

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