Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Reset part Y position script not working?

Hello.

I am currently attempting to make a script where, as soon as it's value is changed to the negatives, it automatically adds 1 until the value is back at 0. I am using a tool, that per swing lesses 1 from the value. I am attemtping to make it automatically regenerate as soon as a number change is detected and it's below 0. This works, however if the value is below -1 (e.g -5), it will first add one, and then duplicate the addition, and so on. So it would go, -5, then -4, then -2 then 2. I am unsure what is causing this, and, with the script, it is also not changing the Part's Y position.

Here is the script I attempted to make:

local IntValue = script.Parent.PositionY
local KelpPart = script.Parent

IntValue:GetPropertyChangedSignal("Value"):Connect(function()
	if IntValue.Value == 0 then
		print("Kelp is already at Y value 0, no need to regenerate")
	else
		if IntValue.Value < 0 then
			repeat
				wait(5)
				IntValue.Value = IntValue.Value + 1 and KelpPart.Position == Vector3.new(KelpPart.Position.X, KelpPart.Position.Y + 1, KelpPart.Position.Z)
				print("Regenerated 1")
			until
			IntValue.Value == 0
		end
	end
end)

I am quite unsure where the source of this issue is coming from, I am experimenting with these things, and would like some help.

Thanks

2 comments

0

konnichiwaa! V ;3

0

hi

1 answer

✓ Accepted answer

While adjusting the IntValue, you'll cause the property-changed signal to consecutively fire, which will begin instantiating a surplus of replenishment loops.

Use a debounce to resolve this issue. Here is a simplified version of your desired program:

--###----------[[VARIABLES]]----------###--
local KelpPart = script.Parent

local IntValue = KelpPart.IntValue 
--// Be more descriptive with your Instance naming!

local RegenerationBuffer = 5

local IsRegenerating = false


--###----------[[LOGIC]]----------###--
IntValue:GetPropertyChangedSignal("Value"):Connect(function()
	if (IntValue.Value >= 0 or IsRegenerating) then return end
	---------------
	IsRegenerating = true
	---------------
	while (IntValue.Value < 0) do
		IntValue.Value += 1
		---------------
		wait(RegenerationBuffer)
	end
	---------------
	IsRegenerating = false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.