Help making a drink empty out

Hello everyone,

I’m making a drinkable and refillable water bottle for a game I’m working on, but I’ve got one problem - the water level in the bottle needs to be able to decrease. I tried a for loop, but it doesn’t quite work. This is because when sizing a part in one direction, you need to change the position by half the increment you’re scaling it by. Therefore, when I move it on the Y axis, it moves slightly off, as I also have a drinking animation that changes the orientation of the part.

Here’s my script:

local tool = script.Parent
local char = nil

local animation = script.Sip

local sipsLeft = script.SipsLeft

local m6d

local tweenService = game:GetService("TweenService")

local waterpart = script.Parent.Tool.Water

local sizes = {
	[0] = Vector3.new(0, 0.4, 0.56),
	[1] = Vector3.new(0.45, 0.4, 0.56),
	[2] = Vector3.new(0.95, 0.4, 0.56),
	[4] = Vector3.new(1.25, 0.4, 0.56)
}

tool:GetPropertyChangedSignal("Parent"):Connect(function()
	if char then
		return
	end
	if tool.Parent.Name == "Backpack" then
		char = tool.Parent.Parent.Character	
	end
end)

tool.Equipped:Connect(function()
	local a:Weld = char:FindFirstChild("Right Arm"):WaitForChild("RightGrip")
	m6d = Instance.new("Motor6D")
	m6d.Parent = char:FindFirstChild("Right Arm")
	m6d.Name = "RightGrip"
	m6d.Part0 = a.Part0
	m6d.Part1 = a.Part1
	m6d.C0 = a.C0
	m6d.C1 = a.C1
	a:Destroy()
end)

tool.Unequipped:Connect(function()
	m6d:Destroy()
end)

tool.Activated:Connect(function()
	if sipsLeft.Value > 0 then
		sipsLeft.Value -= 1

		local animator = char:WaitForChild("Humanoid").Animator
		local load = animator:LoadAnimation(animation)

		load:Play()

		for i = 1, 125 do
			local cur = waterpart.Size
			local curpos = waterpart.Position
			
			waterpart.Size = Vector3.new(cur.X-0.004, cur.Y, cur.Z)
			waterpart.Position = Vector3.new(curpos.X, curpos.Y-0.002, curpos.Z)
			
			task.wait()
		end

		print("Played animation,", sipsLeft.Value, "sips left")
	else
		print("No water left!")
		return
	end
end)

Is there any alternative to a for loop, or any way to change the script to move the part along its local axis?

2 Likes

As I understand, everything in the bottle is welded together via some script like qPerfection?

They are welded to the Handle using WeldConstraints.

Oh, I finally saw it. The water moves in the wrong direction.
How about we try not moving waterpart by the world axis (just adding -value onto Y axis), but move it by the BottomVector?
Try this loop.

		for i = 1, 125 do
			-- We get the exact direction where to move, as the bottle is rotated.
			local waterpartBottomVector = -waterpart.CFrame.UpVector
			
			-- Also changed = to -= for the simplisity sake.
			waterpart.Size -= Vector3.new(0.004, 0, 0)
			waterpart.Position -=  waterpartBottomVector * 0.002
			
			task.wait()
		end

This doesnt take into account rotation.
This will:

waterpart.Size += Vector3.new(0, -0.004, 0)
waterpart.CFrame *= CFrame.new(0, -0.004/2, 0)

This just doesn’t move the water at all.

Can I see what you did for the updated code?

I changed the axis that the water changes its size on to X, as that’s what I found works during testing.

for i = 1, 125 do
			local cur = waterpart.Size
			local curpos = waterpart.Position
			
			waterpart.Size += Vector3.new(-0.004, 0, 0)
			waterpart.CFrame *= CFrame.new(0, -0.004/2, 0)
			
			task.wait()
		end

testing - Roblox Studio (gyazo.com)

That is better, change waterpartBottonVector to:

local waterpartBottomVector = -waterpart.CFrame.RightVector

If it moves up, not down, just remove the minus in it.

1 Like

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