Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Boolean value (true and false value) does nothing?

Asked by 2 years ago
Edited 2 years ago

Hello. I am currently trying to develop a door that can be locked with the enablement of just one simple value. I've tried a lot of things and after some experimenting I've realised that the "Handler" script is doing absolutely nothing at all. The following script is supposed to change something when the value in the model is changed. Here is the code.

local Closed = script.Parent.Closed.Value
local Closed = true -- This line is removed after the 1st edit pretend like it isnt here

if Closed == true then
    --do this if closed true
else    
    --do this if closed false
end

This might just be a small issue but I am not a genius when it comes to scripting, help greatly apprecieated.

[EDIT] : Due to some confusion I will be clearing up some things. Line 2 seems to be totally useless so I will be removing it. The script is not a Local Script, its a regular one. Let me explain what exactly I need from this script. I want that whenever a button is pressed In-Game the value "closed" will be changed. This Script is then supposed to change something judging on whenever the Value is changed to true or false. I've implemented a Button into my game that LITERALLY changes the Value in question when its clicked. And this script does nothing, altough its supposed to "do this if closed true". Instead it sticks to whatever the Value was when the test of the game was started.

0
is it in a local script or server script? TehEpixNeon 0 — 2y
0
im not sure why you have two 'Closed' variables TehEpixNeon 0 — 2y

2 answers

Log in to vote
1
Answered by
2Loos 168
2 years ago
Edited 2 years ago

Hi, good afternoon.

First of all, there are a few errors in your code. The variable "Closed" is used twice. You have already defined Closed, so no need to define it again. Therefore, you may make this change.

local Closed = script.Parent.Closed.Value
Closed = true

The main problem with your code, however, is that you are using a value. The script does not detect if the value is changed. There is a function to detect this change.

:GetPropertyChanedSignal()

When using :GetPropertyChanedSignal(), we must first define the object which we'll be looking inside for a change. In this case, it is the BoolValue "Closed." Inside the parenthesis, we must show which property of the BoolValue we're looking for a change in as well. In this case, it's Closed.Value

We must make some variable changes to account for this. Here is just the variable code and the first line accounting for change.

local Closed = script.Parent.Closed
Closed.Value = true

Notice how instead of just Closed, I follow it by .Value to account for the variable change. Now, we can use the function. Here is how it is used.

local Closed = script.Parent.Closed
Closed.Value = true

Closed:GetPropertyChangedSignal("Value"):Connect(function()
    if Closed.Value == true then
        --Code that will run when the value is true
    else    
        --Code that will run when the value is false
    end
end)

This should work. (It has been tested)

Hope this helps!

0
I had this exact same issue about a year ago. I learned about this function, which is an absolute lifesaver. Glad to pass the knowledge onto you. :) 2Loos 168 — 2y
1
Thank you! I now have a fully functioning door that can be locked by changing the value. Couldn't have done it without you. Attalkiseer 10 — 2y
0
this but please never do `if value == true then` as you can just do `if value then` Benbebop 1049 — 2y
0
Attalkiseer. Thanks! Please mark this post as the answer if it's what helped you :) Thanks! 2Loos 168 — 2y
0
Benbebop, I'm not sure about this. I have been taught to always type out value==true. 2Loos 168 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Not too sure what the problem is here, but I think I might have a solution.


local Closed = script.Parent.Closed.Value --local Closed = true -- Not too sure what the point of this line is, so I'll just leave it out of the code. local Sound = workspace.LockSound -- Make a Sound in the Workspace called LockSound. Make sure you give it an ID. local MouseClick = script.Parent -- Set your script's parent to a ClickDetector inside your door part. MouseClick.MouseClick:Connect(function() if Closed == true then Sound:Play() print("Locked...") --do this if closed true else print("Unlocked!") --do this if closed false end end)

Again, not sure what the problem is, but I hope I helped. Bye!

0
It still doesn't work. Let me explain what exactly I need. When a button is pressed In-Game, that button will turn the Value in the model to false/true. That value will then trigger this script to do something else EVERYTIME the button is pressed and the value gets changed. So far, your script only works when I pause the Test of the game, manually change the value, and then retest it. Attalkiseer 10 — 2y
0
YES YES YES! I know your problem! In your original code, you only check ONCE, when the player joins. Put your code inside a "while wait() do" and your good. LikeableEmmec 470 — 2y

Answer this question