How can I make this script wait for the tween to be done to be used again?

this script keeps trying to play since its being fired by an event which happens everytime I press M1 while standing on it and move around, however It lags A LOT so im trying to only make it work once and after the script is done so it doesnt choke, how can I make this happen?

game.ReplicatedStorage.TouchSnow.OnServerEvent:Connect(function(plr,tsnow)

	local debounce = false
	if debounce then
		debounce = false
		return
	end

	local Snows = script.Parent:GetChildren()

	for _,Snow in pairs(Snows) do
		if Snow:IsA("Part") then

			local tween = game:GetService('TweenService')

			local info = TweenInfo.new(
				5,           --seconds it takes to complete loop
				Enum.EasingStyle.Linear,     --easingstyle (how it moves)
				Enum.EasingDirection.InOut,    --easingdirection (which direction)
				0,                    --times repeated (negative number if u want infinite)
				true,                 --reverse (does it go back to its spot)
				0                      --delay time (stoptime before doing the tween)
			)

			local Goals = {             --YOU CAN CHANGE THESE AND DELETE THE THINGS YOU DON'T WANT TO TWEEN
				Transparency = 1

			}

			local start = tween:Create(Snow, info, Goals) --gets all the info and goals and creates tween

			start:Play()
			debounce = true

			start.Completed:Connect(function(completed)
				if completed == Enum.PlaybackState.Completed then

					print('yes')

				end



			end)
		end


	end
end)



You could wait for TweenBase | Roblox Creator Documentation

2 Likes

Tried multiple times now, I just don’t seem to be able to implement it

start.Completed:Wait()
game.ReplicatedStorage.Build.OnServerEvent:Connect(function(plr,build)

	local Snows = script.Parent:GetChildren()

	for _,Snow in pairs(Snows) do
		if Snow:IsA("Part") then
			Snow.Touched:Connect(function(hit)
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if player then
					print('touch')

					local tween = game:GetService('TweenService')

					local info = TweenInfo.new(
						5,           --seconds it takes to complete loop
						Enum.EasingStyle.Linear,     --easingstyle (how it moves)
						Enum.EasingDirection.InOut,    --easingdirection (which direction)
						0,                    --times repeated (negative number if u want infinite)
						true,                 --reverse (does it go back to its spot)
						0                      --delay time (stoptime before doing the tween)
					)

					local Goals = {             
						Transparency = 1,
						Position = Vector3.new(Snow.Position.X, Snow.Position.Y -2.5, Snow.Position.Z)

					}

					local start = tween:Create(Snow, info, Goals) --gets all the info and goals and creates tween

					start:Play()
					start.Completed:Wait()
					print('done')

				end
			end)
		end
	end
end)

it keeps sending a signal which is not there anymore, i need to find a way to stop the script while its playing the tween and then make it able to play the tween again once its finished

local rs = game:GetService("ReplicatedStorage")
local ts = game:GetService("TweenService")
local snowRE = rs:WaitForChild("TouchSnow")
local debounce = false
local parent = script.Parent
local snows = parent:GetChildren()

snowRE.OnServerEvent:Connect(function(plr,tsnow)
	if debounce then
		return
	end
	debounce = true
	for _, snow in ipairs(snows) do
		if snow:IsA("BasePart") then
			local tweenInfo = TweenInfo.new(
				5,           --seconds it takes to complete loop
				Enum.EasingStyle.Linear,     --easingstyle (how it moves)
				Enum.EasingDirection.InOut,    --easingdirection (which direction)
				0,                    --times repeated (negative number if u want infinite)
				true,                 --reverse (does it go back to its spot)
				0                      --delay time (stoptime before doing the tween)
			)
			
			local current = {
				snow.Transparency,
				snow.Position
			}
			
			local goals = {
				Transparency = 1,
				Position = Vector3.new(snow.Position.X, snow.Position.Y -2.5, snow.Position.Z)
			}

			local start = ts:Create(snow, tweenInfo, goals)

			start:Play()

			start.Completed:Connect(function()
				snow.Transparency = current[1]
				snow.Position = current[2]
				start:Play()
			end)
		end
	end
	task.wait(5) --change this cooldown length if necessary
	debounce = false
end)
1 Like