Script working fine the first time and failing the second time

As the title suggests, I have a script that runs perfectly the first time through, but the second time it seems to skip PART of the first part.

I’m making a round based game and after each round you can obviously play again. A server script is added to a model and that server script adds local scripts to both players. The server script then fires to both clients creating an intro sequence of sorts. After the systems runs its course, both local scripts are destroyed, and then the server script follows.

The intro script includes a black Frame instance being tweened to work as a transition screen,

the first time through, it transitions fine, from covering the screen, to allowing vision, covering again, and then allowing me to see the second player. Perfect.

The second time through, the initial tween to see the first player doesn’t happen but everything else does, such as animations and camera movements. I checked server and local side and the actual frame IS there but just remains at 0 BackgroundTransparency

I’ll try to share the code as best I can, I apologize if it’s rudimentary or even inefficient, I’m a solo developer who is self taught and I’ve been properly scripting for about a year now and haven’t had any sort of “professional help”.

Server Side:

local LocalBridge = game.ReplicatedStorage:WaitForChild("Scripts"):WaitForChild("LocalBridge")
local Active = script:WaitForChild("Active")
local Player1 = false
local Player2 = false
local Gamemode = false
local Room = false

Active.Changed:Connect(function()
	
	Room = script.Parent
	
	task.wait()
	
	Player1 = Room:WaitForChild("Table"):WaitForChild("Data"):WaitForChild("Player1").Value
	Player2 = Room:WaitForChild("Table"):WaitForChild("Data"):WaitForChild("Player2").Value
	Gamemode = Room:WaitForChild("Table"):WaitForChild("Data"):WaitForChild("Gamemode").Value
	
	task.wait()----------------------------------------------------------------------------------------------
	
	local Char1 = game.Workspace:FindFirstChild(Player1)
	local Char2 = game.Workspace:FindFirstChild(Player2)
	
	task.wait()----------------------------------------------------------------------------------------------
	
	local RoomNameVal = Instance.new("BoolValue")
	Char1:WaitForChild("HumanoidRootPart").CFrame = Room:WaitForChild("Player1Intro"):WaitForChild("TeleportPlayer1").CFrame
	Char2:WaitForChild("HumanoidRootPart").CFrame = Room:WaitForChild("Player2Intro"):WaitForChild("TeleportPlayer2").CFrame
	
	task.wait()----------------------------------------------------------------------------------------------
	
	RoomNameVal.Name = Room.Name
	RoomNameVal.Value = true
	
	task.wait()----------------------------------------------------------------------------------------------
	
	local Val1 = RoomNameVal:Clone()
	local Val2 = RoomNameVal:Clone()
	
	task.wait()----------------------------------------------------------------------------------------------
	
	Val1.Parent = Char1
	Val2.Parent = Char2
	
	task.wait()----------------------------------------------------------------------------------------------
	
	local LocalScript1 = game.ReplicatedStorage:WaitForChild("Scripts"):WaitForChild("INTROClient"):Clone()
	local LocalScript2 = game.ReplicatedStorage:WaitForChild("Scripts"):WaitForChild("INTROClient"):Clone()
	local FirstPLR = game.Players:GetPlayerFromCharacter(Char1)
	local SecondPLR = game.Players:GetPlayerFromCharacter(Char2)
	local GameScriptServer = game.ReplicatedStorage:WaitForChild("Gamemodes"):WaitForChild("Server"):WaitForChild(Gamemode):Clone()
	local GameScriptClient1 = game.ReplicatedStorage:WaitForChild("Gamemodes"):WaitForChild("Local"):WaitForChild(Gamemode):Clone()
	local GameScriptClient2 = game.ReplicatedStorage:WaitForChild("Gamemodes"):WaitForChild("Local"):WaitForChild(Gamemode):Clone()
	
	task.wait()----------------------------------------------------------------------------------------------
	
	LocalScript1.Parent = Char1
	LocalScript2.Parent = Char2
	
	task.wait()----------------------------------------------------------------------------------------------
	
	LocalScript1.Enabled = true
	LocalScript2.Enabled = true
	
	task.wait()----------------------------------------------------------------------------------------------
	
	LocalBridge:FireClient(FirstPLR, "MoveToIntroCam1", Room)
	LocalBridge:FireClient(SecondPLR, "MoveToIntroCam1", Room)
	
	task.wait(1)----------------------------------------------------------------------------------------------
	
	LocalBridge:FireClient(FirstPLR, "PlayIntro", Room)
	
	task.wait(5.5)----------------------------------------------------------------------------------------------
	
	LocalBridge:FireClient(FirstPLR, "MoveToIntroCam2", Room)
	LocalBridge:FireClient(SecondPLR, "MoveToIntroCam2", Room)
	
	task.wait(1.7)----------------------------------------------------------------------------------------------
	
	LocalBridge:FireClient(SecondPLR, "PlayIntro", Room)
	
	task.wait(5.5)----------------------------------------------------------------------------------------------
	
	LocalBridge:FireClient(FirstPLR, "SetTheScene", Room)
	LocalBridge:FireClient(SecondPLR, "SetTheScene", Room)
	
	task.wait(2)
	
	Char1:WaitForChild("HumanoidRootPart").CFrame = Room:WaitForChild("Table"):WaitForChild("Chair1"):WaitForChild("TeleportChair1").CFrame
	Char2:WaitForChild("HumanoidRootPart").CFrame = Room:WaitForChild("Table"):WaitForChild("Chair2"):WaitForChild("TeleportChair2").CFrame
	
	task.wait(1)
	
	GameScriptClient1.Parent = Char1
	GameScriptClient2.Parent = Char2
	GameScriptServer.Parent = Room
	print("Game is ready to start!")
	
	task.wait()
	
	GameScriptServer.Enabled = true
	task.wait()
	GameScriptServer:WaitForChild("Active").Value = true
	GameScriptClient1.Enabled = true
	GameScriptClient2.Enabled = true
	
	task.wait(3)
	
	LocalScript1:Destroy()
	LocalScript2:Destroy()
	script:Destroy()
	
end)

Local Side:

local Player = game.Players.LocalPlayer
local TweenService = game:GetService("TweenService")
local LocalBridge = game.ReplicatedStorage:WaitForChild("Scripts"):WaitForChild("LocalBridge")
local Camera = game.Workspace:WaitForChild("Camera")

LocalBridge.OnClientEvent:Connect(function(Key, Room)
	if Key == "MoveToIntroCam1" then
		
		local Cam1 = Room:WaitForChild("Player1Intro"):WaitForChild("Camera1")
		local TransitionFrame = Player.PlayerGui:WaitForChild("TransitionFrame")
		
		task.wait()----------------------------------------------------------------------------------------------
		
		for i,anim in ipairs (Player.Character:WaitForChild("Humanoid"):WaitForChild("Animator"):GetPlayingAnimationTracks()) do
			anim:Stop()
		end
		
		task.wait()----------------------------------------------------------------------------------------------
		
		Camera.CameraType = Enum.CameraType.Scriptable
		task.wait()
		Camera.CFrame = Cam1.CFrame
		
		task.wait(1)----------------------------------------------------------------------------------------------
		
		TweenService:Create(TransitionFrame.BlackFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
		
		task.wait(1)
		
		return
	end
end)

LocalBridge.OnClientEvent:Connect(function(Key, Room)
	if Key == "MoveToIntroCam2" then
		
		local Cam1 = Room:WaitForChild("Player2Intro"):WaitForChild("Camera2")
		local TransitionFrame = Player.PlayerGui:WaitForChild("TransitionFrame")
		TweenService:Create(TransitionFrame.BlackFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 0}):Play()

		task.wait(1)----------------------------------------------------------------------------------------------
		
		for i,anim in ipairs (Player.Character:WaitForChild("Humanoid"):WaitForChild("Animator"):GetPlayingAnimationTracks()) do
			anim:Stop()
		end

		task.wait()----------------------------------------------------------------------------------------------

		Camera.CameraType = Enum.CameraType.Scriptable
		task.wait()
		Camera.CFrame = Cam1.CFrame

		task.wait(1)----------------------------------------------------------------------------------------------

		TweenService:Create(TransitionFrame.BlackFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
		
		task.wait(1)

		return
	end
end)

LocalBridge.OnClientEvent:Connect(function(Key, Room)
	if Key == "PlayIntro" then
		
		local Humanoid = Player.Character:WaitForChild("Humanoid")
		local IntroAnim = Player:WaitForChild("DataFolder"):WaitForChild("Equipped"):WaitForChild("IntroAnimation").Value
		
		task.wait()
		
		local AnimID = game.ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Intros"):WaitForChild(IntroAnim)
		
		task.wait()----------------------------------------------------------------------------------------------
		
		local AnimTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(AnimID)
		
		task.wait()----------------------------------------------------------------------------------------------
		
		AnimTrack:Play()
		
		while AnimTrack.TimePosition < 5 do
			task.wait()
		end
		
		AnimTrack:AdjustSpeed(0)
	end
end)

LocalBridge.OnClientEvent:Connect(function(Key, Room)
	if Key == "SetTheScene" then
		
		local Humanoid = Player.Character:WaitForChild("Humanoid")
		local TransitionFrame = Player.PlayerGui:FindFirstChild("TransitionFrame")
		local SeatedAnim = Player:WaitForChild("DataFolder"):WaitForChild("Equipped"):WaitForChild("SittingAnimations").Value
		
		task.wait()
		
		local AnimID = game.ReplicatedStorage:WaitForChild("Animations"):WaitForChild("AnimPacks"):WaitForChild(SeatedAnim):WaitForChild("Sitting")
		TweenService:Create(TransitionFrame.BlackFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 0}):Play()
		
		task.wait()
		
		local AnimTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(AnimID)
		
		task.wait(2)
		
		for i,anim in ipairs (Player.Character:WaitForChild("Humanoid"):WaitForChild("Animator"):GetPlayingAnimationTracks()) do
			anim:Stop()
		end
		
		task.wait()
		
		AnimTrack:Play()
		
		task.wait(2)
		
		Camera.CameraType = Enum.CameraType.Follow
		TweenService:Create(TransitionFrame.BlackFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()

		task.wait(1)

		return
	end
end)

If anybody has any sort of idea why it’s happening or has any advice in general please let me know. Any help means the world to me and goes quite a long way since I get most of my knowledge from Forums and then Documentation.

Thank you for your time and you patience, stay safe.

ACherryPy-

(Sidenote: Would there be a way to create unique remote events for each script? Lets say max server size if 50 people and there are 50 people stressing out that RemoteEvent, would it buffer or would roblox be able to manage that? Again I have no idea so that’s why I ask. If you guys have any advice or help whatsoever, it means the world to me.)

UPDATE: I tried just not deleting any of the scripts and letting Respawn take care of that and that made no difference.

The tween issue is caused by BlackFrame.BackgroundTransparency not being reset before starting a new tween. sidenote question: It’s really not the number of, it’s how many times it called in succession. You want to stay away from systems that constantly fire all the time. I forget now, but there is actually a number per player per times fired in succession. So it IS limited to a point.

1 Like

So what should I avoid? Tweening too much? Gonna try what you suggested by the way, one moment.

What confuses me is that when the player respawns after the round, ALL UI besides stats gets wiped, so TransitionUI no longer exists and neither does BlackFrame, and it gets created per usage, same for the scripts, that’s what confuses me, I thought I made it as clean as possible, I’m not sure why it’s doing this.

Issue is still persisting, not really sure how to fix it I mean, the UI is parented to the Player’s PlayerGUI at the time of the tween, and the first time works just fine, the locating of the Frame uses WaitForChild() not creating any variables, it should in theory work just fine. I’m not sure what the issue is.

I narrowed it down to the server script.

	LocalBridge:FireClient(FirstPLR, "MoveToIntroCam1", Room)
	LocalBridge:FireClient(SecondPLR, "MoveToIntroCam1", Room)

This part of the code just SOMETIMES doesn’t fire, and I have no clue why. It works perfectly the first time through, and then it just magically becomes chance after that. All scripts, UI, Tweens, and Models are fresh and not reused. At least now I know the Tweening and the UI itself is innocent, now I just need to figure out why this is happening. wish I knew why.

I added a bunch print functions to the script itself.

LocalBridge.OnClientEvent:Connect(function(Key, Room)
	if Key == "MoveToIntroCam1" then
		
		local Cam1 = Room:WaitForChild("Player1Intro"):WaitForChild("Camera1")
		local TransitionFrame = Player.PlayerGui:WaitForChild("TransitionFrame")
		print("Values made!")
		
		task.wait()----------------------------------------------------------------------------------------------
		
		local BlackFrame = TransitionFrame:WaitForChild("BlackFrame")
		print("Black Frame found and valued!")
		
		for i,anim in ipairs (Player.Character:WaitForChild("Humanoid"):WaitForChild("Animator"):GetPlayingAnimationTracks()) do
			anim:Stop()
		end
		print("Animations stopped!")
		
		task.wait()----------------------------------------------------------------------------------------------
		
		BlackFrame.BackgroundTransparency = 0
		print("Black frame transaprency set to 0!")
		
		task.wait()----------------------------------------------------------------------------------------------
		
		Camera.CameraType = Enum.CameraType.Scriptable
		task.wait()
		Camera.CFrame = Cam1.CFrame
		print("Camera moved to Cam1!")
		
		task.wait(1)----------------------------------------------------------------------------------------------
		
		print("Tweening Black Frame now!")
		TweenService:Create(BlackFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
		
	end
end)

And on the second pass through, it’s just not firing whatsoever. Not a SINGLE print function triggering, so that makes me think that it’s just not firing to client even once. I slightly tweaked the server script if that matters at all.

task.wait()----------------------------------------------------------------------------------------------
	
	local LocalScript1 = game.ReplicatedStorage:WaitForChild("Scripts"):WaitForChild("INTROClient"):Clone()
	local LocalScript2 = game.ReplicatedStorage:WaitForChild("Scripts"):WaitForChild("INTROClient"):Clone()
	local FirstPLR = game.Players:GetPlayerFromCharacter(Char1)
	local SecondPLR = game.Players:GetPlayerFromCharacter(Char2)
	local GameScriptServer = game.ReplicatedStorage:WaitForChild("Gamemodes"):WaitForChild("Server"):WaitForChild(Gamemode):Clone()
	local GameScriptClient1 = game.ReplicatedStorage:WaitForChild("Gamemodes"):WaitForChild("Local"):WaitForChild(Gamemode):Clone()
	local GameScriptClient2 = game.ReplicatedStorage:WaitForChild("Gamemodes"):WaitForChild("Local"):WaitForChild(Gamemode):Clone()
	
	task.wait(0.1)----------------------------------------------------------------------------------------------
	
	LocalScript1.Parent = Char1
	LocalScript2.Parent = Char2
	
	task.wait(0.1)----------------------------------------------------------------------------------------------
	
	if LocalScript1.Parent == Char1 and LocalScript2.Parent == Char2 then
		LocalScript1.Enabled = true
		LocalScript2.Enabled = true
	end
	
	task.wait(0.1)----------------------------------------------------------------------------------------------
	
	if LocalScript1.Enabled == true and LocalScript2.Enabled == true then
		LocalBridge:FireClient(FirstPLR, "MoveToIntroCam1", Room)
		LocalBridge:FireClient(SecondPLR, "MoveToIntroCam1", Room)
	end
	
	task.wait(1.1)----------------------------------------------------------------------------------------------

I made it a point so that it only continues when parented and when enabled, yet it still doesn’t fire. If anybody can help me that’d mean the world to me. I’ve tried asking AI and it’s of no help. As if it ever really was but you know what I mean. I’ve exhausted my options.

If anybody can help me out, thank you so much.

(Taking a break for the night, it’s 1:58AM, I haven’t eaten since like 2PM and I’ve got a headache. Have a great rest of your night you guys.)

Why are you using Event.OnClientEvent:Connect() many times? You can use it just once.

1 Like

Can I? I had no idea. As I said I’m self taught so I just don’t know any better. The videos and websites I learned from taught me I could do it like this so that’s just how I’ve always done it. I’ll make that change and hope it fixes my problem.

All they are the same events, aren’t they?
You can rewrite like this:
LocalBridge.OnClientEvent:Connect(function(Key, Room)
if
then
if
then
if
then
end
)

1 Like

Oh man I had no idea, I’ll definitely update my code tomorrow, thank you Ocean. When I do update it if that fixes the issue I’ll come running back to let you know. Thanks again.

I changed my code on the local side to what you suggested and the script obviously still works fine but the issue of the server skipping over the first phase is still happening and I’m not quite sure why. What bothers me is I’m sure it’s like one super tiny miniscule thing I overlooked and when I find it I’ll beat myself up for it.

UPDATE

I got it working, I basically just added a failsafe so that the client has to send back to the server confirmation of the script running and unless that confirmation is received it just runs again and keeps running until confirmation is received. I’ll polish this off going forward, adding some sort of timeout maybe but for now it works just fine.

Thank you guys for all your help and the advice you gave me.

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