How do I stop a tween after the player presses a button?

Hello, I am trying to make it so when player presses E the parts around them slowly tween down in size, but when they press E again it stops. I’m trying to figure out the best way to do this, currently, I send the parts as a table to the server and the server tweens the parts, but I’m unsure on how to cancel the tween without doing anything tacky.

Client

game.ReplicatedStorage.Remotes.DemonEatingRemote:FireServer(DeadPlayerParts) 

Server

local TweenService = game:GetService("TweenService")
local TweenInf = TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
game.ReplicatedStorage.Remotes.DemonEatingRemote.OnServerEvent:Connect(function(player,DeadParts)
	local char = player.Character or player.CharacterAdded:Wait()
	if char:WaitForChild("PlayerInputMiscellaneous"):WaitForChild("Eating").Value == false then
		char:WaitForChild("PlayerInputMiscellaneous"):WaitForChild("Eating").Value = true 
		for i,v in pairs (DeadParts) do
			local Tween = TweenService:Create(v,TweenInf,{Size = Vector3.new(0,0,0)})
			Tween:Play()
			-- I can make an individual tween for each part and not loop through all the parts, but I think theres a better way to do it?
		end
	end
end)

You can use the Tween:Pause() and then reset the tween part’s properties one by one.

1 Like

Yeah, I know what tween pause and cancel are, BUT, when I made the tween inside the for i,v in pairs loop. Which means each tween variable is overwritten. I could make a tween for each individual part but that is not efficient, and I want to see if there is a better way to do it.

You have to tween each individual part. There isn’t a way to tween a model and it’s just as unstable as tweening a model manually. Each part will need to be tweened individually. If you’re doing position, you can weld the other parts on. For size you have to do a loop.

Put the created Tween objects into a table, so you still have a reference to them, when you need to cancel them.

-- Pseudo code
local arrayOfTweens = {}

for _,v in ipairs(Deadparts) do
  local tween = TweenService:Create(...)
  table.insert(arrayOfTweens, tween)
  tween:Play()
end

-- .. and then when you need to cancel all the tweens:
for _,t in ipairs(arrayOfTweens) do
  t:Cancel()
end

Note that the above pseudo-code does not take into account, which tween objects belong to which player. For that you should make a dictionary, where each element contain the table of tween-objects for that particular player.