Same boolean variable prints different value in different scripts

Hello! I’ve been trying to make an elevator system, and I am currently making a boolean value called “onfloor1” that changes depending on the Y position of the floor of the elevator. However, even though I am printing the same boolean, it prints differently.
The script that accurately prints what floor it is on is this:


while 1==1 do
	local onfloor1 = game.Workspace.onfloor1.Value
	local ElevatorPos = game.Workspace.Elevator.Position
	local Y = ElevatorPos.Y
	
	ElevatorPos = game.Workspace.Elevator.Position
	Y = math.round(ElevatorPos.Y)
	print(Y)
	
	


	if Y==11 then
		onfloor1 = true
		print(onfloor1)
		print("yes it is, it is on floor1.")
	end
	if Y==22 then
		print("yes it is, it is on floor 2.")
		onfloor1 = false
		print(onfloor1)
	end
	wait(3)
end

When it is on floor one, it prints “yes it is, it is on floor1”, and when it’s on floor two, it prints “yes it is, it is on floor 2.” If it is not on either floor, it doesn’t print anything, which is fine.
However, this script:

while 1==1 do
	print(game.Workspace.onfloor1.Value)
	wait(0.3)
end

will always print false.
I am super confused with this.
edit: Also, here is the output:

You are overriding the variable onFloor1 that should be an instance with a Boolean. You need to set the value of the instance to True.

if Y==11 then
		onfloor1.Value = true
end

You also need to set onFloor1 to the instance, not the value of the instance.

Full code

while 1==1 do
	local onfloor1 = game.Workspace.onfloor1
	local ElevatorPos = game.Workspace.Elevator.Position
	local Y = ElevatorPos.Y
	
	ElevatorPos = game.Workspace.Elevator.Position
	Y = math.round(ElevatorPos.Y)
	print(Y)
	
	


	if Y==11 then
		onfloor1.Value = true
		print(onfloor1)
		print("yes it is, it is on floor1.")
	end
	if Y==22 then
		print("yes it is, it is on floor 2.")
		onfloor1.Value = false
		print(onfloor1)
	end
	wait(3)
end

Setting the variable to the Value of the instance will just copy the value over, so changing the variable later won’t change the instance.

1 Like

If you look in the script variable he already put the value in it no need to re put that again

It is assigned, but when trying to change the value of it to true, they are setting the variable to true instead of the boolValue overriding the reference

1 Like

Never mind. I just noticed that onFloorOne is equal to the Boolean.Value. That would just set it to false instead of allowing it to be freely changed so that would need to be a reference to the instance, not the value of the instance.

1 Like

I found on the way it print is when the first script is executing its executed very fast and the other take a delay of 0.3 to execute so try removing the wait in it

Sorry, I kind of don’t understand what you are saying. Are you saying you figured out what was wrong? Also: I’ve tried doing

local onfloor1 = game.Workspace.onfloor1.Value
while 1==1 do
	print(onfloor1)
	wait(0.3)
end

so I defined OnFloorOne the same way in both scripts, however it still prints false.

If I remove the wait in it, it will crash roblox studio. I didn’t really see a point of making it shorter or longer, so I just chose 0.3. I just tested it using wait() without the 0.3, and the same thing happens.

When you set the variable to the boolValue.Value it essentially just copies it. So when you change onFloor1 you aren’t really changing the boolValue. You are just overwriting the Boolean the variable was. To actually change the value, you need to say boolValue.Value = value. So at the definition don’t include the .Value and put that where you want to change it instead.

Or more simply stated, at the beginning of the script when you are setting up onFloor1. You are essentially saying onFloor1 = false because that’s what the boolValue.Value is equal to. So the other script never sees the update because you actually aren’t updating it. You are just changing a local variable.

I see what you mean now, however when I do

local onfloor1 = game.Workspace.onfloor1.Value
while 1==1 do
	onfloor1 = game.Workspace.onfloor1.Value
	print(onfloor1)
	wait()

end

It still spam prints false. I also don’t see why the other script that I showed at the beginning of the post would print the floor it was on correctly.

Just read the comments

local onfloor1 = game.Workspace.onfloor1.Value --This just sets it to false because onfloor1.Value = false
while 1==1 do
	onfloor1 = game.Workspace.onfloor1.Value --And now this line is setting the value to what it already is
--but since this value is a boolean, and not a reference to the instance boolValue it doesn't actually change anything other than the local variable.  So the boolValue is uneffected by this
	print(onfloor1)
	wait()

end
local onfloor1 = game.Workspace.onfloor1 --This stores a reference to the boolValue instance
while 1==1 do
	onfloor1.Value = true --This sets the value of the boolValue instance to true
        
	print(onfloor1)
	wait()

end

for this script:

local onfloor1 = game.Workspace.onfloor1 --This stores a reference to the boolValue instance
while 1==1 do
	onfloor1.Value = true --This sets the value of the boolValue instance to true
        
	print(onfloor1)
	wait()

end

Wouldn’t adding value after the first line and deleting the Value after onfloor1. do the same thing? And doing print(onfloor1) just prints “onfloor1”.

No. It doesn’t do the same thing. When you want to change the value in the boolValue you need to specify it. Otherwise the code wouldn’t be able to distinguish if you wanted to change the value of the boolValue or if you wanted to change what the local variable is connected to.

When you set onFloor1 = game.Workspace.onFloor1.Value you are setting the variable to false. When you try changing it by saying onFloor1 = true, you simply change the local variable to true. It doesn’t effect the boolValue at all. You can think of it as though you are copying the value of boolValue to the variable then discarding the boolValue itself.

And the reason why it isn’t printing correctly is simply because I forgot to have it print onFloor1.Value. Right now it is printing the instance onfloor1 rather than it’s attached value.

2 Likes

tlr22 already answered this, but it might be good to hear someone else try to explain it too.
No, it’s not the same. The way that you had it would copy the value into a variable, and you would be changing the variable - not the value - when you adjusted onfloor1 in the script.
It prints onfloor1 now because onfloor1 is game.Workspace.onfloor1, in your print you should specify print(onfloor1.Value) to compensate for that.

2 Likes

So using this script:

local onfloor1 = game.Workspace.onfloor1 --This stores a reference to the boolValue instance
while 1==1 do
	onfloor1.Value = true --This sets the value of the boolValue instance to true

	print(onfloor1.Value)
	wait()

end

When I change the value of the boolean using the method below: (clicking the box on and off) it just keeps printing true. Does clicking value while ingame actually do anything?
Screen Shot 2021-02-17 at 9.18.54 PM
Also sorry for the late response, I had to do something else.
edit: Oh never mind, I’m dumb

Well, its repeating multiple times a second and constantly setting the boolValue.Value to true before printing. This is because you haven’t put your if statements back in to really decide what it should be. I just put true as a placeholder for the example. Clicking that value won’t really do things because it’s adjusted in loop by the script before it prints.

Yeah, I just came back and forgot where I was.
so this simple script:

local onfloor1 = game.Workspace.onfloor1 
while 1==1 do


	print(onfloor1.Value)
	wait()

end

Does not work. I’m not sure why.