Script only works second time it's ran and onwards

Hey developers pretty much my issue is that this script doesn’t work the first time it’s ran then it works every time after that when its run. The script is the exact same for each time it runs so no values are getting changed, heres the part that’s messed up (Everything else works perfectly)

tween2.Completed:Connect(function(o)
						if o == Enum.PlaybackState.Completed then 
							local clone = Shockwave:Clone()
							clone.Parent = workspace
							local tween = TweenService:Create(clone, ShockWaveinfo, goals)
							tween:Play()
							tween.Completed:Connect(function(playBackState)
								if playBackState == Enum.PlaybackState.Completed then 
									clone:Destroy()
								end
							end)

Edit: The shockwave part is located in Server Storage

You are sure that block doesnt run at all the first time? or maybe its the second tween inside of it?, you checked with prints?

if o == Enum.PlaybackState.Completed then 
warn("did run in here")

I am certain the second tween runs very smooth and everything else works apart from shockwave appearing the first time. But I will use some prints soon to see if I can find problem though I highly doubt it will give me anything if it runs the second time and onwards. Could this possibly be a roblox error?

I dont understand, what are we calling the “second tween”?

This tween:

local tween = TweenService:Create(clone, ShockWaveinfo, goals)
tween:Play()

or this tween:
tween2:Play()

I dont think its from roblox, and yup mandatory to check with prints to know which line it actually reaches

Oh, another thing, maybe you want to declare the tween.Completed, before to play the tween
(thats not the issue, just keeping things in order)

tween.Completed:Connect(function(playBackState)
    if playBackState == Enum.PlaybackState.Completed then 
 	    clone:Destroy()
    end
end)
tween:Play()

nah the clone is meant to be destroyed when the tween has finished playing and i was referring to second tween as tween2 which is a foot stomping

Yeah, I was referring as the second tween to the one which is inside the tween2.
So the tween inside tween2 doesnt trigger the first time that code runs?
First you need to be sure that block is never reached:

if o == Enum.PlaybackState.Completed then 
warn("did run in here")

With that script you provided I dont see any issue that could cause that part is not reached, the problem is somewhere else I think

Ive managed to pinpoint it to this line

tween2.Completed:Connect(function(playbackState)

(I changed the parameter from o btw)
So i’ve done a whole bunch of prints and tween 2 plays but its never registered as completed. But the second time its played it registers perfectly

heres my full script

local MarketplaceService = game:GetService("MarketplaceService")
local TweenService = game:GetService("TweenService")
local ServerStorage = game:GetService("ServerStorage")

local Debounce = false
CoolDown = script.Parent.Parent.CoolDown.CoolDown

local Shockwave = ServerStorage:WaitForChild("ShockWave")
local Leg = game.Workspace.TrollStompBoss.LeftLeg
local head = game.Workspace.TrollStompBoss.Head.Head
local ShockWaveinfo = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local goals = {
	Size = Vector3.new(1.75, 194.5, 283.25),
	Position = Vector3.new(1853.58, 14.396, -510.175)
}
local info2 = TweenInfo.new(0.6, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, true, 0)
local info3 = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
local goals2 = {
	Position = Vector3.new(1851.527, 28.998, -507.832)
}
local tween2 = TweenService:Create(Leg, info2, goals2)
local goals3 = {
	Orientation = Vector3.new(24.46, -98.931, 1.936)
}
local goals4 = {
	Orientation = Vector3.new(-25.514, -100.574, 1.953)
}
local tween3 = TweenService:Create(head, info3, goals3)
local tween4 = TweenService:Create(head, info3, goals4)

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, 122099059) then 
		if Debounce == false then 
			CoolDown.Enabled = false
			script.Parent.Color = Color3.new(1,0,0)
			Debounce = true 
			tween3:Play()
			tween3.Completed:Connect(function(playBackState)
				if playBackState == Enum.PlaybackState.Completed then 
					head.Sound:Play()
					head.Roar.Transparency = 0
					task.wait(3.03)
					head.Roar.Transparency = 1
					tween4:Play()
					task.wait(1)
					tween2:Play()
					task.wait(1.5)
					Leg.Sound:Play()
					tween2.Completed:Connect(function(playbackState) --- doesnt register this the first time the buttons pressed (I removed the print to figure this out)
						if playbackState == Enum.PlaybackState.Completed then 
							print("Tween 2 is completed")
							local clone = Shockwave:Clone()
							print("Cloned shockwave")
							clone.Parent = workspace
							local tween = TweenService:Create(clone, ShockWaveinfo, goals)
							tween:Play()
							print("Playing tween on shockwave")
							tween.Completed:Connect(function(playBackState)
								if playBackState == Enum.PlaybackState.Completed then 
									clone:Destroy()
									
								end
							end)
						end
					end)
				end
				
			end)
			task.wait(10)
			script.Parent.Color = Color3.new(1, 1, 0)
			Debounce = false
					
		elseif MarketplaceService:UserOwnsGamePassAsync(player.UserId, 122099059) and Debounce == true then
		CoolDown.Enabled = true 
			local random = math.random(1, 5)
			if random == 1 then
				CoolDown.TextLabel.Text = "CoolDown"
			elseif random == 2 then 
				CoolDown.TextLabel.Text = "Be Patient!"
			elseif random == 3 then 
				CoolDown.TextLabel.Text = "Chill"
			elseif random == 4 then 
				CoolDown.TextLabel.Text = "pls no"
			elseif random == 5 then 
				CoolDown.TextLabel.Text = "STOP CLICKING ME!!!"
			end 
			task.wait(1)
			CoolDown.Enabled = false
		end
	else 
		game:GetService("ReplicatedStorage").Events.VIPPopUp:FireClient(player)
		end
end)

I think the issue is the thing about I was saying about ordering.
Probably cause you are using task.wait() and you are playing the tween2 before to declare that when that tween completes perform a function.
The second time it runs, finally completed event is connected, thats the reason why the second time it actually completes.
You should declare the complete event before to play the tween:

local MarketplaceService = game:GetService("MarketplaceService")
local TweenService = game:GetService("TweenService")
local ServerStorage = game:GetService("ServerStorage")

local Debounce = false
CoolDown = script.Parent.Parent.CoolDown.CoolDown

local Shockwave = ServerStorage:WaitForChild("ShockWave")
local Leg = game.Workspace.TrollStompBoss.LeftLeg
local head = game.Workspace.TrollStompBoss.Head.Head
local ShockWaveinfo = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local goals = {
	Size = Vector3.new(1.75, 194.5, 283.25),
	Position = Vector3.new(1853.58, 14.396, -510.175)
}
local info2 = TweenInfo.new(0.6, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, true, 0)
local info3 = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
local goals2 = {
	Position = Vector3.new(1851.527, 28.998, -507.832)
}
local tween2 = TweenService:Create(Leg, info2, goals2)
local goals3 = {
	Orientation = Vector3.new(24.46, -98.931, 1.936)
}
local goals4 = {
	Orientation = Vector3.new(-25.514, -100.574, 1.953)
}
local tween3 = TweenService:Create(head, info3, goals3)
local tween4 = TweenService:Create(head, info3, goals4)

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, 122099059) then 
		if Debounce == false then 
			CoolDown.Enabled = false
			script.Parent.Color = Color3.new(1,0,0)
			Debounce = true 
			tween3:Play()
			tween3.Completed:Connect(function(playBackState)
				if playBackState == Enum.PlaybackState.Completed then 
					head.Sound:Play()
					head.Roar.Transparency = 0
					task.wait(3.03)
					head.Roar.Transparency = 1
					tween4:Play()
					task.wait(1)
					
					--[[ Heres the issue]]
					-- Declare the completed event first
					tween2.Completed:Connect(function(playbackState)
						if playbackState == Enum.PlaybackState.Completed then 
							print("Tween 2 is completed")
							local clone = Shockwave:Clone()
							print("Cloned shockwave")
							clone.Parent = workspace
							local tween = TweenService:Create(clone, ShockWaveinfo, goals)
							tween:Play()
							print("Playing tween on shockwave")
							tween.Completed:Connect(function(playBackState)
								if playBackState == Enum.PlaybackState.Completed then 
									clone:Destroy()

								end
							end)
						end
					end)
					
                 -- Play the tween now
					tween2:Play()
					
					--[[ Heres the issue]]
					
					task.wait(1.5)
					Leg.Sound:Play()

				end

			end)
			task.wait(10)
			script.Parent.Color = Color3.new(1, 1, 0)
			Debounce = false

		elseif MarketplaceService:UserOwnsGamePassAsync(player.UserId, 122099059) and Debounce == true then
			CoolDown.Enabled = true 
			local random = math.random(1, 5)
			if random == 1 then
				CoolDown.TextLabel.Text = "CoolDown"
			elseif random == 2 then 
				CoolDown.TextLabel.Text = "Be Patient!"
			elseif random == 3 then 
				CoolDown.TextLabel.Text = "Chill"
			elseif random == 4 then 
				CoolDown.TextLabel.Text = "pls no"
			elseif random == 5 then 
				CoolDown.TextLabel.Text = "STOP CLICKING ME!!!"
			end 
			task.wait(1)
			CoolDown.Enabled = false
		end
	else 
		game:GetService("ReplicatedStorage").Events.VIPPopUp:FireClient(player)
	end
end)

EDIT:
The tween2 info, says it should play during 0.6 secs, so it finished before you connected the completed event which occurs after the task.wait(1.5).
Thats why the error, cause it ended before you declare what to do when completed

1 Like

dang you’ve helped so much! The script had been working before but I adjusted the time of the tween. I really appreciate this.

1 Like

Im glad I could help :yum: 30kchaaaars

1 Like

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