How would I make a typewriter script that doesn't break when someone enters/leaves the game?

This issue has been annoying to me for so long. I tried asking a friend, but they haven’t gotten back at all, so I am asking you.
I currently use this below, and the issue described in the title occurs with it.

> local Text = "Hello!" 
> for i, player in pairs(game.Players:GetPlayers()) do
> 	for i = 1, #Text do
> 		player.PlayerGui.Text.TextLabel.Text = string.sub(Text,1,i)
> 		wait(0.01)
> 	end

Any help is appreciated

For those of us who don’t know (Me), what exactly is a typewriter script?

A typewriter script is like when you type on a computer. The word isnt immediately shown up. It looks as if someone is typing on a keyboard

This announcement here really helps get the idea

Oh, ok, so for like displaying a text message to the player, you have it ‘type’ out onto the screen?

So what do you mean by ‘break’ when someone enters or exits?

What happens is that the script is connected to the script that runs the whole game. When a player joins or leaves a game when the typewriter is in effect, the script breaks, causing a permanent freeze. Nothing happens after that

And as for moving it to a different script. I tried using a StringValue in ReplicatedStorage to have the text, and then have a script inside the gui to do the typewriting using the string value.

I honestly can’t think of another way to do it. I am not good at all with Remote Events and stuff like that so…

Is the effect happening inside of a ScreenGui, that is on the client?
If so, you only need to send a signal from the sever to the client telling to to display the typing, and if you need that to stop, send a signal for it to stop.

If you mean that the server is waiting for the typing to finish on a client, before the server continues, you should not be doing that. The server can wait an estimated time for the clients to finish reading the text, or you can wait for the client to send a signal saying ‘im done reading’ before continuing, but have a timeout, so if that client takes too long, you can stop waiting.

How is it set up, is it a single player game, or multiplayer? When exactly does the text happen?

No the whole server experiences the issue. If one person does the script-breaking action, as mentioned, it breaks the entire game for everyone in the server

use fireallclients and do it on the client it will look better

1 Like

You can wrap it in a pcall, or just simply do

local Text = "Hello!" 
for i, player in pairs(game.Players:GetPlayers()) do
	coroutine.wrap(function()
		for i = 1, #Text do
			if player:FindFirstChild("PlayerGui") and player.PlayerGui:FindFirstChild("Text") then
 				player.PlayerGui.Text.TextLabel.Text = string.sub(Text,1,i)
				wait(0.01)
			end
		end
	end)()
end

But as @FrostNovazz said, it is better to use a remote event for this.

It seems the problem in your script is that you are accessing player.PlayerGui.Text.TextLabel.Text at a variety of arbitrary times without verifying its existence.

There’s two potential solutions to this problem:

  1. Set a variable to player.PlayerGui.Text.TextLabel before the typewriter effect loop begins. Even if the player leaves, the instance will remain in existence until the effect is completed, though you would be wasting resources completing the effect.
  2. Add a check at the start of the for loop as to whether the player is still in the game. This will require either using a bunch of FindFirstChild checks or checking whether player.Parent == nil.

I would also recommend handling the typewriter effect on the client, which circumvents this issue entirely.

The way you would implement this with remotes is to have the server fire all clients when the typewriter effect needs to be triggered, passing a string with the text that should be animated.

-- Server
typeWriterRemote:FireAllClients(stringToAnimate)
-- Client
typeWriterRemote.OnClientEvent(function(stringToAnimate)
   ...
end)

This way, the effect would happen for all players simultaneously without coroutines or the task library, and if a player leaves, it could only ever affect their client scripts (not to mention that the effect would be much smoother).

Thank you so much for the help!

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