Roblox script, int value and if statement help

im trying to write a piece of code that means if a int value is = to 0 then it makes the part dissapear. this is what i have so far but it wont work. can anyone help me?

image

1 Like
  1. There is no delay in your while loop, results in a crash.
  2. Show us what health is
1 Like

Could you send the place file or add some details? as I don’t know what the “Health” instance is and how your game is structured.

health is an int value that is a child of the main object, basically when you click the object it takes -1 value of health and im trying to make it so that if the object reaches 0 or bellow health then it disappears.

health is an int value that is a child of the main object, basically when you click the object it takes -1 value of health and im trying to make it so that if the object reaches 0 or bellow health then it disappears. thats basicly what all the other info i have is

You could use GetPropertyChangedSignal instead of using a loop.

Using this, will detect anytime the given property changes.

local Health = script.Parent.Health

Health:GetPropertyChangedSignal("Value"):Connect(function()
   if Health.Value == 0 then
      script.Parent.Transparency = 1
	  script.Parent.CanCollide = false
   end
end)

Try this:

--//Variables
local Part: Part = script.Parent
local Health: IntValue = Part.Health

--//Functions
Health:GetPropertyChangedSignal("Value"):Connect(function()
    if Health.Value <= 0 then
	  Part.Transparency = 1
	  Part.CanCollide = false
    end
end)
Health:GetPropertyChangedSignal("Value"):Connect(function()
       if Health.Value <= 0 then

             Part.Transparency = 1
	         Part.CanCollide = false
       end
	
end)

That will make the part goes transparent and cancollide on false every time the value changes. He only wants it to be changed when it hits 0.

Oops forgot to include that if statement.

Try this -

local Health:IntValue = script.Parent.Health

Health:GetPropertyChangedSignal("Value"):Connect(function()
   if Health.Value == 0 then
      script.Parent.Transparency = 1
	  script.Parent.CanCollide = false
   end
end)

What happens here:
local Health:IntValue = script.Parent.Health
Here, we simply assign our Health variable[ the :IntValue makes sure it is indeed an intvalue, no necessary]

Health:GetPropertyChangedSignal("Value"):Connect(function()
This will trigger each time the Value of our Health [intvalue] changes.

if Health.Value == 0 then script.Parent.Transparency = 1 script.Parent.CanCollide = false end
here just simply do our code, the if statement is there to make sure this will run only when Health is at 0.

An alternative way:


local Health:IntValue = script.Parent.Health

Health.Changed:Connect(function()
   if Health.Value == 0 then
      script.Parent.Transparency = 1
	  script.Parent.CanCollide = false
   end
end)

Great response 2 things though, just in case use <= 0 second thing is more of a preference since it requires less typing using .Changed rather than GetPropertyChangedSignal("Value")

Now some might say .Change will run on anything that gets change, like archivable and not only the Value property. Those who say that would be wrong though (in the case of Value Instances), Value instances (such as IntValue) are special in the fact that the .Changed function only runs when the Value gets change.

You can also just used Changed because this is an IntValue, which has a special Changed event.

1 Like