Script checking for certain number value [NEED HELP PLEASE]

I’m trying to create a script that checks a NumberValue until the value is changed to a certain number. For example, in the script below I am having the script repeat until the NumberValue equals 1.

The NumberValue is set to 3. I want the script to detect when the NumberValue is set to 1 then make “Heart_One” and “Heart_Two” visibility = false.

However, whenever I play test and change the NumberValue the script does not stop repeating and nothing happens.

you could try repeating the if

repeat
if value.Value>2 then
--Script this
end
until value.Value==1

Since the code within the loop is set up in a way to only update the properties of other objects once the end condition is met (being that HeartValue.Value == 1), a loop wouldn’t be necessary for this use case.

Because NumberValues have a built in Changed event, which fires every time its Value property changes, it’d be more effective to utilize that so the code will only run when necessary.


Is the code in the screenshot included within a server script (e.g. standard Script with its RunContext property set to Legacy or Server, or is it a LocalScript / a Script with its RunContext set to Client?

Additionally, when you change the NumberValue while playtesting, are you doing this manually via the Explorer & Properties windows or is a script within the game making those changes?


For example, if the code was being run on the server side but the NumberValue was being updated locally, the server-sided code would not detect that its Value was changed, so the loop would continue running until it is changed on the server-side to the desired value.


Here’s an example revision that also includes print statements so that you’ll be able to see in the Output when it detects changes to the NumberValue:

Example Revision


-- Variables above here

HeartValue.Changed:Connect(function(newValue)
    print("HeartValue was updated to: "..tostring(newValue))

    if newValue == 1 then
        Heart_One.Visible = false
        Heart_Two.Visible = false

        underLine.BackgroundColor3 = Color3.new(0.0705882, 0, 0)
        warn("NewValue was 1; the UI has been updated!")
    end
end)

The code via the screenshot is inside of a standard script.
I am changing the NumberValue through the explorer for testing purposes (this will be done via script in the future)

I give the code you put in and see if it works

1 Like

Tried out the code you put and it ended in the same result.

This is a image showing I set the value to == 1
The script inside the frame “Underline” is the script that controls this whole thing.
I tried changing the RunContext from legacy to server and still nothing.
Also I don’t think I included the output but I did check the output and there was no errors there, always as, there was no print inside the output either.

EDIT: Also forgot to mention that this is all in the workspace.

1 Like

The most viable way to check if the value has been changed is by using :GetPropertyChangedSignal
.Changed fires when any property of the NumberValue changes.

HeartValue:GetPropertyChangedSignal("Value"):Connect(function(): ()
    --// Use `HeartValue.Value`
end)

you forgot to add a condition after until

Ah, that’s the exact example situation I outlined during my post when explaining one of the possible reasons it might not be working:

While playtesting in Roblox Studio, if you don’t swap the “Current (View):” in the Testing tab to the “Server” side prior to changing the NumberValue via the Explorer, then its Value is being changed locally, which is why the server-sided script contained within the Frame is not detecting that the Value was changed.

It is generally recommended to handle UI from the client (with some reasons as to why being summarized in the post I’ll link below, right below where it says “Useful Resource” ), so I would strongly encourage swapping out the Script within the “Underline” Frame to be a LocalScript, instead.

  • However because the code within LocalScripts cannot run unless it is a descendant of specific containers, there are different ways of making this possible.

    1. You could continue using the same Script but change its RunContext to Client so that it’ll be able to run locally while being able to remain in the same spot. This would be the quickest and simplest change if the HeartValue will only ever be changed from the client-side. If there is a possibility that the server may update the HeartValue, this would not be viable because it would detect the HeartValue changing for every player at the same time (since there is only one HeartValue versus having a separate one for each player).

    2. Another option would be to place the SurfaceGui into the StarterGui Service, and then set its Adornee property to the Health_UI part within the Workspace. This property effectively “attaches” the GUI to the selected part while allowing it to remain within the player’s PlayerGui folder so that it can be modified using a LocalScript. Technically, with this setup, the server could be responsible for updating the HeartValue individually for each player and it would work, but it’d still be recommended to handle these types of UI-specific changes locally.


However, during the course of standard gameplay, if the HeartValue would typically be updated from a Server Script and you want to maintain that workflow, there are several ways to go about that.

  1. Relocate the HeartValue to somewhere completely separate for each player, such as the ReplicatedStorage, and then update it from there while the newly swapped out LocalScript simply listens for changes to it using the NumberValue.Changed event, like how it was shown in my example revision.

    • Or, consider turning the HeartValue into an Attribute / custom property of the Player object, instead.
  2. Utilize a RemoteEvent to communicate between the Server and an individual Client when the HeartValue should be updated. This way, the server will decide when it should be updated but the client will be the one that modifies the HeartValue to the value that the server told it to (as well as locally detecting from the LocalScript that a change was made and that the UI should be updated if its new value is equal to 1).

Useful Resource


In the case of most objects, the Object.Changed event fires when any property of that object changes.

However, when it comes to all ValueBase objects (including IntValue, NumberValue, StringValue, etc.), they utilize a completely separate Changed event that only detects when its Value property is modified, passing through the new value that it was updated to.

As I linked during my original reply in this topic, the documentation for the NumberValue.Changed event on the Roblox Creator Hub mentions the following:

Changed

This event fires whenever the NumberValue.Value property is changed.

This event, like other changed events, can be used to track when an NumberValue changes and to track the different values that it may change to.

For instance, this even may be useful in games that rely on NumberValues to track game states and values, such as item IDs.

Equivalent changed events exist for similar objects, such as ObjectValue and StringValue, depending on what object type best suits the need.

Parameters

value: number
The new value after the change.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.