Tween animation does not stop

I did a tweening animation and when a value is true inn an if statement it is supposed to stop the animation and let the subject where it last was and i even added prints wich showed up in the output but it does nothing . I got no error,i tried changing the cancollide and anchor propretys . I even tried using the AI.

local TweenService = game:GetService("TweenService")
local Dribble = game:GetService("ReplicatedStorage"):FindFirstChild("DribbleBallForward")
local Stop = game.ReplicatedStorage:FindFirstChild("StopTween")
local who = game.Workspace.Values.Who
local sto = game.Workspace.goala.sto
Dribble.OnServerEvent:Connect(function(player, hum, Speedy, Energy,forwardVector)

	
	local Target = Speedy.Value + ((Speedy.Value * 0.2) * 5)
	local ball = game.Workspace.Ball
	local meshPart = ball:FindFirstChild("MeshPart")

	meshPart.Anchored = true
	meshPart.CanCollide = false
	if not meshPart then
		
		return
	end

	 -- Assuming the lookVector points forward
	local targetPosition = meshPart.Position + forwardVector * Target 

	local tweenInfo = TweenInfo.new(3)
	local tween = TweenService:Create(meshPart, tweenInfo, { Position = targetPosition })
	

	tween:Play()
	who.Value = player.Name
	print(who.Value)
	while true do
		wait()
		print("outgo")
	if sto.Value == true then
			print("ongoing")
			tween:Pause()
			tween:Destroy()
			meshPart.Position = meshPart.Position
			--make the tweening stop 
			sto.Value = false
			print("mar")
		end
		end
	wait()
	meshPart.CanCollide = true
	meshPart.Anchored = false
	print("tween")
	
	print("DELETE")
	print("FALSE")
end)

6 Likes

Try
tween:Cancel()

instead of doing
tween:Pause()
tween:Destroy()

I tried it but unfotunatly it still does not work .

So these print statements run?

yes they are running but nothing visually happens .

Nothing will be shown because it gets to tween:Pause() faster than it starts tweeting. The problem it repeats might be with
while true do try making it different.

I assume this is for dribbling a basketball. If the client fires the Dribble event multiple times quickly, then that could be the issue. If it does the rapid firing of the event, it creates a whole new tween and restarts the tween so quickly that even after pausing the tween, it starts another right back up and continues.
You could try adding this statement at the top of the function before anything else runs:

if sto.Value == true then return end

If the event isn’t called rapidly then there may be another issue so let me know if that’s not the case.

Also, since you’re using a while true, is this code ever reached:

wait()
meshPart.CanCollide = true
meshPart.Anchored = false
print(“tween”)

print("DELETE")
print("FALSE")

Yes it’s not calling rapidly scince it is a soccer dribbling and even when i do it one single time it still does this.

Try this and let me know how it goes:

Dribble.OnServerEvent:Connect(function(player, hum, Speedy, Energy,forwardVector)
	
	local ball = game.Workspace.Ball
	local meshPart = ball:FindFirstChild("MeshPart")
	if not meshPart then return end
	
	local Target = Speedy.Value + ((Speedy.Value * 0.2) * 5)
	meshPart.Anchored = true
	meshPart.CanCollide = false

	-- Assuming the lookVector points forward
	local targetPosition = meshPart.Position + forwardVector * Target 
	local tweenInfo = TweenInfo.new(3)
	local tween = TweenService:Create(meshPart, tweenInfo, { Position = targetPosition })
	
	tween:Play()
	
	sto.Changed:Connect(function()
		if not sto.Value then return end
		
		tween:Pause()
		tween:Destroy()
		
		meshPart.CanCollide = true
		meshPart.Anchored = false
		
		sto.Value = false
	end)
	
	who.Value = player.Name
end)

I changed the while loop to an event. And I cleaned up some of the things at the top.

It worked! thank you,you made my day!

1 Like

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