Spamming a tween service causes model to drift ever so slightly

this code snippet is activated by a mouse click

function rotation(model, value)
	local info = TweenInfo.new(1, Enum.EasingStyle.Quad)
	local target = {CFrame = model.PrimaryPart.CFrame * CFrame.Angles(math.rad(value), 0, 0)}
	
	local tween = tweenservice:Create(model.PrimaryPart, info, target)
	
	tween:Play()
	task.wait(1)
	
end

when i quickly change ‘value’ from 1 to -1, the model being tweened slowly shifts away from its intended position

intended position

unintended position
inuntedd

1 Like

The debounce just doesn’t work. You just made a debounce variable but it doesn’t check if the variable is true first. Unless your debounce is used in another part of code.

So the problem is coming from tweens being set to the models current Cframe * angles.

So what’s happening is you click first, the model starts moving maybe halfway, then you click again, so it starts moving back but not back to the original position but past it because it never reached the full tween. Now this will probably result in some weird glitches because you are running 2 opposite tweens at once

The way I’d suggest would be to have your left/right tween created before the function (while the track is still stationary), then play these tweens in the function on an if statement. Also I’d put a line in there that cancels the opposite tween when you play the new one

how would you cancel a tween mid animation?

Although if like above me said if you want it to work with debounce, then put an if statement at the start and tie it in with the completed signal

Similar to tween:Play() just use tween:Cancel()

local debounce = false

function rotation(model, value)

	If debounce == false then
		debounce = true 
		local info = TweenInfo.new(1, Enum.EasingStyle.Quad)
		local target = {CFrame = model.PrimaryPart.CFrame * CFrame.Angles(math.rad(value), 0, 0)}
	
		local tween = tweenservice:Create(model.PrimaryPart, info, target)
	
		tween.Completed:connect(function()
			debounce = false		
		end)
		tween:Play()
	end
end

Here’s a way you could fix your debounce, depends whether you want to be able to switch tracks immediately out easily until it’s finished changing

1 Like

i tried this, but spamming it still makes it drift

1 Like

Make sure the debounce variable is set up properly, print before the if statement the value of debounce.

Also make sure to destroy the tween, it’ll likely get collected with garbage collection but it’s a good habit to get into. That’d just be tween:Destroy()

1 Like