Why do the position & value doesn't change?

Hello, I want to make a hunger bar that will never stop going up until the value is at 1, which kills us.
I made a NumberValue and made a variable to the position of the bar. However, it doesn’t go up.

while wait(1) do
	local pos = script.Parent.Position.X.Scale
	local value = script.Parent.HungerValue.Value
	value = value + 0.016
	pos = value
	print(pos)
	if pos == 1 then
		local h = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
		if h then
			h.Health = 0
		end
	end
end

The problem is that you set the variable “value” to a number, not to an object. When you set “value” equal to the hungervalue.value, it is setting it as a number. So when you add 0.016 to value, it is just making a number bigger, not the actual hungervalue. To fix this, set “value” equal to hungervalue, not hungervalue.value. Then when you wish to add 0.016 to it, add to hungervalue.value.

You’re “increasing” value with the same calculations over and over.

local pos = script.Parent.Position.X
local value = script.Parent.HungerValue.Value
while task.wait(1) do	

	value += 0.016
	pos = pos - value
	print(pos)

end

The problem is that he is setting “value” to a number instead of the actual HungerValue instance. This means that when he adds to the “value”, he is not actually changing the value of HungerValue, but instead, a local variable.

This is the problem, change it to:

script.Parent.HungerValue.Value += .016

It didn’t worked! Don’t you have an another solution?

Just pointing out the loop keeps doing the same calculation as some of that shouldn’t be in the loop itself. Also, to use pos and not Value as the one getting changed to move.

Yours didn’t worked too! It seems like it updated the value and position but not in the GUI.

Just showing to take some of that out of the loop … that wasn’t a working code.

while task.wait(1) do
	local hunger = script.Parent.HungerValue
	hunger.Value += 0.016
	print(hunger.Value)
	if hunger.Value > 1 then
		hunger.Value = 1
		local h = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
		if h then
			h.Health = 0
		end
	end
	script.Parent.Position = UDim2.new(hunger.Value) -- ask me if the position is wrong, because im not sure how your UI looks
end

You cannot set properties or values implictly (indirectly).
Also, you cannot set values in a UDim2, you need to make a new one instead. Same thing for values like Vector3, Vector2, Color3, etc.

1 Like

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