Trying to make typewriter effect, but typewriter sound seems to play twice

I’m at a loss for ideas, but are you sure the duplicated prints aren’t running because you forgot to disconnect after the player pressed the button?

1 Like

What do you mean by disconnect?

In this script, you’re assigning a RBXSCRIPTCONNECTION to whenever said button is clicked. You should disconnect said connection in order to prevent two text effects running. This is the only other idea I have, unfortunately. Though, heres an example of how you can do this.

local TextLabel = script.Parent
local Button = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").OptionYes
local Audio = game.ReplicatedStorage.TypeWriter
local WaitTime = 0.08
local connection

local messages = {
	[1] = "So, this is the place Joey told me about...", -- put your full message here
	[2] = "This old building is the perfect place to host something of value.", -- put your full message here
	[3] = "Now it's time to open these rusted doors and hopefully find something worth my time.", -- put your full message here
	[4] = "What is this place?...", -- put your full message here
	[5] = "...", -- put your full message here
	[6] = "The door is gone.", -- put your full message here
	[7] = "I need to find some way out of here.", -- put your full message here
}

local function StartNewGame()
       connection:Disconnect()
	task.wait(5)

	-- Create a loop that loops through all of your messages
	for index, message in ipairs(messages) do
		-- Test for the message that activates the visibility
		if index == 4 then
			TextLabel.Visible = false
			task.wait(8)
			TextLabel.Visible = true
		end
		if index == 5 then
		WaitTime = 1
		end
		if index == 6 then
			WaitTime = 0.08
		end

		-- Normal character / typewriter loop
		for i = 1, #message, 1 do
			local CloneAudio = Audio:Clone()
			local PS = CloneAudio:FindFirstChildOfClass("PitchShiftSoundEffect")
			PS.Octave = math.random(95, 105)/100
			print(i)
			CloneAudio.Parent = game.SoundService
			TextLabel.Text = string.sub(message, 1, i)
			CloneAudio:Play()
			CloneAudio.Ended:Connect(function()
				CloneAudio:Destroy()
			end)
			task.wait(WaitTime) -- Wait between characters
		end
		task.wait(3) -- Wait 3 seconds after each message has finished typing
	end

	task.wait(3)
	TextLabel.Visible = false
end

connection = Button.MouseButton1Click:Connect(StartNewGame)
1 Like

I noticed you said that there’s this connection when the button is clicked, and that disconnecting it might work. You gave an example of where to disconnect this connection, but it didn’t work. I noticed when I destroyed the other script, it was still like it was there. There was no source, and I figured I would also disconnect the script before destroying it. This finally worked and the number was only printed once. You have no idea how relieved I am to finally have found a fix to this.

This is the local script under StarterPlayerScripts:

local TextLabel = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui2").Text
local Button = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").OptionYes
local Audio = game.ReplicatedStorage.TypeWriter
local WaitTime = 0.08
local connection

local messages = {
	[1] = "So, this is the place Joey told me about...", -- put your full message here
	[2] = "This old building is the perfect place to host something of value.", -- put your full message here
	[3] = "Now it's time to open these rusted doors and hopefully find something worth my time.", -- put your full message here
	[4] = "What is this place?...", -- put your full message here
	[5] = "...", -- put your full message here
	[6] = "The door is gone.", -- put your full message here
	[7] = "I need to find some way out of here.", -- put your full message here
}

local function StartNewGame()
	connection:Disconnect()
	task.wait(5)

	-- Create a loop that loops through all of your messages
	for index, message in ipairs(messages) do
		-- Test for the message that activates the visibility
		if index == 4 then
			TextLabel.Visible = false
			task.wait(8)
			TextLabel.Visible = true
		end
		if index == 5 then
			WaitTime = 1
		end
		if index == 6 then
			WaitTime = 0.08
		end

		-- Normal character / typewriter loop
		for i = 1, #message, 1 do
			local CloneAudio = Audio:Clone()
			local PS = CloneAudio:FindFirstChildOfClass("PitchShiftSoundEffect")
			PS.Octave = math.random(95, 105)/100
			print(i)
			CloneAudio.Parent = game.SoundService
			TextLabel.Text = string.sub(message, 1, i)
			CloneAudio:Play()
			CloneAudio.Ended:Connect(function()
				CloneAudio:Destroy()
			end)
			task.wait(WaitTime) -- Wait between characters
		end
		task.wait(3) -- Wait 3 seconds after each message has finished typing
	end

	task.wait(3)
	TextLabel.Visible = false
end

if script.Parent.Name == "StarterPlayerScripts" then
	connection:Disconnect()
	script:Destroy()
end

connection = Button.MouseButton1Click:Connect(StartNewGame)
1 Like

Glad you fixed it, but you do realize I mentioned disconnecting previous connections in my initial reply right? :joy:

2 Likes

I did, however your mentioned Button.MouseButton1Click:DisconnectAll() while @4141429 mentioned local connection and connection:Disconnect(). These operate differently. When I tried Button.MouseButton1Click:DisconnectAll() the script stopped entirely, while when I tried connections:Disconnect() it only stopped the other script.

1 Like

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