Help With Script

Hello, devs! I am having an issue with a script in my game currently. It is a script which chooses a video to display when the timer counts down to 0. I used a tutorial by TheDevKing to help me achieve the cooldown system. However, in Studio, when 0 is reached, it hangs and I have to close Studio and open it again. I tested the game on the Roblox Client and it acts a bit differently. It actually plays the video selected but after a while, kicks you from the game claiming you got disconnected on PC and mobile. Here is the script. Thanks for any help!

local shortId = 0

local chosenShort = ""

local randomNum = math.random(1, 4)

if randomNum == 1 then
	shortId = game.ReplicatedStorage.Shorts["Eat!"].Video
	chosenShort = game.ReplicatedStorage.Shorts["Eat!"]
end
if randomNum == 2 then
	shortId = game.ReplicatedStorage.Shorts["News Station"].Video
	chosenShort = game.ReplicatedStorage.Shorts["News Station"]
end
if randomNum == 3 then
	shortId = game.ReplicatedStorage.Shorts["Lagoon Cliff Waterfall"].Video
	chosenShort = game.ReplicatedStorage.Shorts["Lagoon Cliff Waterfall"]
end
if randomNum == 4 then
	shortId = game.ReplicatedStorage.Shorts["Fight Loop"].Video
	chosenShort = game.ReplicatedStorage.Shorts["Fight Loop"]
end


local InSessionLength = chosenShort.TimeLength
local IntermissionLength = 70
local InSession = game.ReplicatedStorage.Room1InShort
local Status = game.ReplicatedStorage.Room1Status

local function timer()
	for i = IntermissionLength, 0, -1 do
		InSession.Value = false
		wait(1)
		Status.Value = "Room 1 Intermission: ".. i .." seconds left to enter!"
	end
	for i = InSessionLength, 0, -1 do
		InSession.Value = true
		wait(1)
		Status.Value = "Room 1 Watching Short!"
	end
end

local maxval = script.Parent.MaxAllowed

local space = 0

script.Parent.ProximityPrompt.ActionText = "Enter - "..space.."/"..maxval.Value

spawn(timer)

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	if space < 10 then
	    player.Tickets.Value = player.Tickets.Value - 1
	    space = space + 1
	    player.Character.HumanoidRootPart.Position = Vector3.new(-187.076, -6.307, -23.176)
		script.Parent.ProximityPrompt.ActionText = "Enter - "..space.."/"..maxval.Value
	end
end)

InSession.Changed:Connect(function()
	if InSession.Value == true then
		game.Workspace.MovieTheater.Room1Entrance.EnterPrompt.ProximityPrompt.Enabled = false
		game.Workspace.Part.SurfaceGui.VideoFrame.Video = shortId
		game.Workspace.Part.SurfaceGui.VideoFrame.Playing = true
		game.Workspace.Part.SurfaceGui.VideoFrame.Played:Connect(function()
			InSession.Value = false
			game.Workspace.Part.SurfaceGui.VideoFrame.Video = ""
		end)
	else
		game.Workspace.MovieTheater.Room1Entrance.EnterPrompt.ProximityPrompt.Enabled = true
		space = 0
		script.Parent.ProximityPrompt.ActionText = "Enter - "..space.."/"..maxval.Value
	end
end)
3 Likes

I looked for a bit didnt see much, but most often thats described by stopping the thread. Make sure you dont have any infinite loops, is there any other script that listens to the IntValue that is being modified?

1 Like

This is the only script that listens to the BoolValue being modified indicating that the intermission has ended to play a video.
(Sorry for any slow typing. I’m on mobile rn.)

1 Like

I’d imagine it was probally something that happens when the video plays since the code that has the loop runs inside a coroutine. Also replace wait() with task.wait(), could you show the code that plays the video?

1 Like
local shortId = 0

local chosenShort = ""

local randomNum = math.random(1, 4)

if randomNum == 1 then
	shortId = game.ReplicatedStorage.Shorts["Eat!"].Video
	chosenShort = game.ReplicatedStorage.Shorts["Eat!"]
end
if randomNum == 2 then
	shortId = game.ReplicatedStorage.Shorts["News Station"].Video
	chosenShort = game.ReplicatedStorage.Shorts["News Station"]
end
if randomNum == 3 then
	shortId = game.ReplicatedStorage.Shorts["Lagoon Cliff Waterfall"].Video
	chosenShort = game.ReplicatedStorage.Shorts["Lagoon Cliff Waterfall"]
end
if randomNum == 4 then
	shortId = game.ReplicatedStorage.Shorts["Fight Loop"].Video
	chosenShort = game.ReplicatedStorage.Shorts["Fight Loop"]
end

The part aboves manages selection of video to be played.

InSession.Changed:Connect(function()
	if InSession.Value == true then
		game.Workspace.MovieTheater.Room1Entrance.EnterPrompt.ProximityPrompt.Enabled = false
		game.Workspace.Part.SurfaceGui.VideoFrame.Video = shortId
		game.Workspace.Part.SurfaceGui.VideoFrame.Playing = true
		game.Workspace.Part.SurfaceGui.VideoFrame.Played:Connect(function()
			InSession.Value = false
			game.Workspace.Part.SurfaceGui.VideoFrame.Video = ""
		end)
	else
		game.Workspace.MovieTheater.Room1Entrance.EnterPrompt.ProximityPrompt.Enabled = true
		space = 0
		script.Parent.ProximityPrompt.ActionText = "Enter - "..space.."/"..maxval.Value
	end
end)

This part above detects once the cooldown is finished to play the video.

1 Like

Aha! You make connections that in change modfit the InSession.Value. Theese connections are never disconnected so when it is modified twice.It creates a infnite loop of event calling.

So to be able to fix the issue, I have to remove that line?

Try to remove double connections, it seems as if it lead to a infinite loop of calling. So hopefully if you remove that it shouldnt freeze.

Thanks for the help! I discovered it was a typo.