Variable Behavior

I have this code with no errors:

local var = true

if var then
	event:FireAllClients({
		RETURN = "VARTRUE",
		VALUE = false
	})
else
	event:FireAllClients({
		RETURN = "VARFALSE",
		VALUE = true
	})
end

(Value should be true for var = false and false for var = true)

The issue is that var is being set to false even though I have defined it as true. There are no other parts in the script setting the value to false. Does anyone know why?

1 Like

Assuming event is firing a remote, and value is a ValueBase’s value, then this code would be equivalent correct?

local var = true

if var then
	var = false
	print("variable changed to false")
else
	VALUE = true
	print("variable changed to true")
end

This works completely fine and is seemingly equivalent to what you have posted, just without ValueBase changes and remotes. Is it possible that you could provide a less shortened version - because nothing here seems to be incorrect, unless I’m misunderstanding your question.

I somehow managed to fix it, but thanks for replying.

this could may work?

local var = false -- start value

var = var == false -- Togglebetween true and false

print(var and "Its true" or "Its false") --small if statement
var = not var

Is a more idiomatic way of flipping a boolean.

6 Likes