Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Script isn't updating from bool value?

Asked by
RookPvPz -20
1 month ago
Edited by incapaxx 1 month ago

Hello,

I'm making an alarm script, however, the settings will not update from the settings folder and bool value setup to tell the script if the normal alarm is on, or the gas alarm is on.

```lua while true do

wait(0.1)

if game.Workspace.Settings.Alarms.Valu e== true then
    wait(0.1)
    script.Parent.BottomLight.Enabled = true
    script.Parent.TopLight.Enabled = true
    script.Parent.Spinner.Disabled = false
else
    wait(0.1)
    script.Parent.BottomLight.Enabled=false
    script.Parent.TopLight.Enabled=false
    script.Parent.Spinner.Disabled=true
    script.Parent.Orientation = Vector3.new(90, -90, 0)

    if game.Workspace.Settings.Gas.Value == true then
        wait(0.1)
        script.Parent.Parent.Lamp.BrickColor = BrickColor.new("Really blue")

        script.Parent.TopLight.Color = Color3.new(0, 0, 255)

        script.Parent.BottomLight.Color = Color3.new(0, 0, 255)
        wait(0.1)
        script.Parent.Orientation = Vector3.new(90, -90, 0)
        script.Parent.BottomLight.Enabled = true
        script.Parent.TopLight.Enabled = true
        workspace.Settings.Alarms.Value = false
    else 
        if game.Workspace.Settings.Gas.Value == false then
            wait(0.1)
            script.Parent.Parent.Lamp.BrickColor = BrickColor.new("CGA brown")
            script.Parent.TopLight.Color = Color3.new(170, 85, 0)
            script.Parent.BottomLight.Color = Color3.new(170, 85, 0)
            script.Parent.BottomLight.Enabled = false
            script.Parent.TopLight.Enabled = false
            script.Parent.Orientation = Vector3.new(90, -90, 0)

        end
    end
end

end ```

0
Where is the bool value located and is this script local? Gameplay28 69 — 1mo
0
No, this script is server, and the bool value is in workspace > Settings. RookPvPz -20 — 1mo
0
put it in a code block this time LoganboyInCO 135 — 1mo
0
I better be getting compensation for fixing your unindented code. incapaxx 3086 — 1mo

1 answer

Log in to vote
2
Answered by 1 month ago
Edited 1 month ago

Ok, I see two main issues with your script.

  1. You don't use any variables
  2. You use a while loop instead of the useful events Roblox has provided you with

Your bug is probably stemming from the second issue that I pointed out, but solving the first issue can make your life as a programmer so much easier, and your programs could become more efficient as a result of using variables.


Let's dive right in! I notice that you use script.Parent a significant number of times. Do your hands never get tired? There's a solution that everyone knows about, but never uses. It's called a variable. Now, the issue I notice a lot is that YouTubers and tutorials explain how to use a variable, but never when. As a general rule, if you're writing the same code twice, it should be in a variable or a function depending on what you are doing. In your case, you can make a variable, such as par(which would stand for parent) and use that instead of script.Parent. Once you create the variable, you can save yourself some typing time by using Ctrl + h to replace all occurrences of script.Parent with your variable name (which was par in my example).


Ok, on to fixing the actual issue. You're using a while loop to detect changes, which is not optimal nor is it recommended by any experienced programmer on Roblox. The reason it's not recommended is that there are two ways that allow you to detect changes with little to no work. They are Changed and GetPropertyChangedSignal("PropertyName"). Here's an example with both in use:

local boolVal = Instance.new("BoolValue")
boolVal.Value = false
-- first way:
boolVal.Changed:Connect(function()
    print(boolVal.Value)
end
boolVal.Value = true
-- output --> 'true'

-- other (I recommend this one) way:
boolVal:GetPropertyChangedSignal("Value"):Connect(function()
    print(boolVal.Value)
end
boolVal.Value = false
-- not output because the value did not change
boolVal.Value = true
-- output --> 'true'

I'm assuming you can figure it out from here.


If you use these methods and it still doesn't work, then it's probably because you are making the changes manually from the client, and the server cannot detect those changes (even in studio) because of filtering enabled. The way to fix this for testing is to click the button in studio that says current server (or something like that) so that you can make the changes from the server. Or, you can write a different script to make the changes in order to test.


One final note: I noticed that the indentation in your script was pretty poor. I suggest indentation because it improves readability and ease in debugging. You can read more on that here.


I hope this helps! If you have any additional questions, feel free to leave them in the comment section below.

0
My discussion about script.Parent was an example. I encourage variables for your other repeated code as well. Roblox has already made a global variable 'workspace' for your convenience. You can use that instead of game.Workspace. AetherProgrammer 50 — 1mo
Ad

Answer this question