Why does the value increase more than my specified variable?

so I have got a pump. when you use a proximity prompt, it collects the liquid(also only when it’s full). Problem is, I’ve tried to troubleshoot why I get 10-15-20 more liquid than I normally would and failed.

If anybody can help me find the bug, it’d be greatly appreciated!
I have also marked down some print statements to see when this occured, and it’s after BEEvent (when the player has 20) to BEEvent2 (the player has 30 now, but the Collect event is disabled.)
Script 1

--{{ Services & Variable Locations }}--
local TweenService = game:GetService("TweenService")

local BE 		   = game.ReplicatedStorage:WaitForChild("BindableEvent")
local Collect 	   = game.ReplicatedStorage:WaitForChild("CollectEvent")
local aether 	   = script.Parent
local valve 	   = script.Parent.Parent.Valve.ValveHandle

--{{ Numeric Variables }}
local refillTime   = 5 -- How much it takes to refill it
local aetherValue  = 5 -- How much Aether can it store

--{{ Tweening }}--
local TweenDown    = TweenService:Create(aether, TweenInfo.new(1), {Size = Vector3.new(0.1, 4.7, 4.7)})
local TweenUp      = TweenService:Create(aether, TweenInfo.new(refillTime), {Size = Vector3.new(10, 4.7, 4.7)})

--/ Main gameloop
TweenDown:Play()
repeat wait() until TweenDown.Completed
TweenUp:Play()
repeat wait() until TweenUp.Completed

BE.Event:Connect(function(player)
	if aether.Size == Vector3.new(10, 4.7, 4.7) then
		if TweenUp.Completed then
			TweenDown:Play()
			print("BEEvent "..player.leaderstats.Aether.Value)
			Collect:Fire(player)
			print("BEEvent2 "..player.leaderstats.Aether.Value)
		end
	end
end)

TweenDown.Completed:Connect(function()
	wait(5)
	TweenUp:Play()
end)

Script 2

--{{ Variable Locations }}--
local BE 			  = game.ReplicatedStorage:WaitForChild("BindableEvent")
local Collect 		  = game.ReplicatedStorage:WaitForChild("CollectEvent")
local stopanim 		  = game.ReplicatedStorage:WaitForChild("StopAnimGlobal")
local ProximityPrompt = script.Parent
local Valve 		  = script.Parent.Parent
local Root 			  = script.Parent.Parent.Root

--{{ Debounces }}--
local playerHolding -- primary debounce

--{{ Numeric Variables }}--
local refillTime 	  = 5 -- How much it takes to refill it
local aetherValue 	  = 100 -- How much Aether can it store

--{{ Animation }}--
local animation 	  = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9396489358"

--{{ COLLECT (1) & Prompt Debounce}}
ProximityPrompt.Triggered:Connect(function(player)
	ProximityPrompt.Enabled = false
	BE:Fire(player)
	print("PromptTriggered "..player.leaderstats.Aether.Value)
	wait(3)
	ProximityPrompt.Enabled = true
end)

--{{ While Holding }}--
function Hold(player)
	local HRP 	    = player.Character:WaitForChild("HumanoidRootPart")
	local hum 	    = player.Character:WaitForChild("Humanoid")
	local lookatp   = script.Parent.Parent.LookAtPart
	HRP.CFrame 	    = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))

	local valveAnim = hum.Animator:LoadAnimation(animation)
	
	if playerHolding then return end
	playerHolding   = player
	valveAnim:Play()
	wait()
	
	HRP.CFrame      = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))
	hum.AutoRotate  = false
	HRP.Position    = Root.Position
	HRP.Anchored    = true
	
	stopanim.Event:Connect(function(player)
		valveAnim:Stop()
	end)
	
	repeat
		Valve.CFrame = Valve.CFrame*CFrame.Angles(-0.1, 0, 0)
		task.wait(1/40)
	until not playerHolding 
end

--{{ When Stopped Holding }}
function HoldEnded(player)
	local HRP 		  = player.Character:WaitForChild("HumanoidRootPart")
	local hum 		  = player.Character:WaitForChild("Humanoid")
	if playerHolding == player then
		playerHolding = nil
	end
	stopanim:Fire()
	
	hum.AutoRotate 	  = true
	HRP.Anchored   	  = false
end


--{{ COLLECT (2) }}
--[[Collect.Event:Connect(function(player)
	player.leaderstats.Aether.Value = player.leaderstats.Aether.Value + aetherValue
	print("CollectEvent "..player.leaderstats.Aether.Value)
end)]]

ProximityPrompt.PromptButtonHoldBegan:Connect(Hold)
ProximityPrompt.PromptButtonHoldEnded:Connect(HoldEnded)

Screenshot_2

1 Like

can you tell where you output the values?

1 Like

I don’t really know what you mean by that, but I could probably answer something related
The liquid script fills up the tank, the Prompt script sends a bindable event (if the player finished loading the prompt) to the Liquid script to empty the tank, which, in turn, the liquid script sends out another bindable event back to the Prompt script to give the player a value.
A little bit confusing, I know. Sorry

Edit: Also, right now the part of the script which should give me liquid in the leaderstats is this: (located in the Prompt Script)

--{{ COLLECT (2) }}
--[[Collect.Event:Connect(function(player)
	player.leaderstats.Aether.Value = player.leaderstats.Aether.Value + aetherValue
	print("CollectEvent "..player.leaderstats.Aether.Value)
end)]]

I forgot to mention this is currently commented, so the extra liquid doesn’t come from this part, which is weird

1 Like

Also, I looked at the LiquidScript and from

Collect:Fire(player)

it seems to get that extra value, yet I still don’t know a fix

1 Like

Use the math library functions min or clamp if you need to clamp a value within a maximum bound.

2 Likes

Thank you, can I get an example on how to use those in this context? In this case does it have something to do with math.clamp(0,1,2)?

1 Like

https://developer.roblox.com/en-us/api-reference/event/TweenBase/Completed

Just so you’re aware, this is an RBXScriptSignal object, not a Boolean property.

repeat wait() until TweenUp.Completed

This ‘repeat until’ loop is only going to iterate once as its condition is already satisfied.

1 Like

ok, ty! I still don’t really understand the math.clamp(x,y,z) part, tho.

1 Like

math.clamp() clamps the value provided as its first argument between the bounds as indicated by its second & third arguments. The second argument is the lower bound and the third argument is the upper bound. It returns the clamped value.

math.clamp(1, 2, 3) --Value is less than the lower bound so the function returns the lower bound.
math.clamp(3, 1, 2) --Value is greater than the upper bound so the function returns the upper bound.
math.clamp(2, 1, 3) --Value is within both bounds so the function returns the value itself.

1 Like