Typewriter not working?

Ok, so basically I have this Typewriter effect that’s meant to work but it’s not. I’m not sure if it’s because the function is in the wrong place or because of other reasons, but any help would be greatly appreciated, thanks.

local message = script.Parent.Timer -- Timer 
local players = game:GetService("Players")

game.ReplicatedStorage.Remotes.RemoteEvent.OnClientEvent:Connect(function() -- Timer Remote
	print("got")
	for i = 3, 0, -1 do -- Countdown
		message.Text = i
		print(i)
		wait(1)
		if i == 0 then
			print("finished") 
			local Chosen = players:GetPlayers()[math.random(#players:GetPlayers())] -- Randomly choose a player
			print(Chosen.Name) -- Stops at this print
			--- Anything after the above print doesn't run ---
			wait(.1)
			--- TypeWriter Function
			local function typewrite(object,text,length)
				for a = 1, #text, 1 do
					object.Text = string.sub(text, 1, 1)
					wait(length)
				end
				
				typewrite(message, " The Killer is: " .. Chosen.Name, 0.1) 
				print("Done") -- This print doesn't run
			end
		end
	end
end)

I think you should do it like this.

local function typewrite(object, text, length)
    for i = 1, text:len() do
        object.Text = string.sub(text, 1, i);
        wait(length);
    end
end

You set it the third argument of [color=teal]string.sub[/color] to [color=teal]1[/color], which is why it should only be stuck at one. But if you set it to I, it should work.

1 Like

Oh shoot, I forgot to mention that they’re are different.

The countdown should use for i, and the typewrite something else. But, regardless it doesn’t print or show on the gui. Basically the entire function doesn’t run.

Is the entire thing just yielded, or are there any errors?

I think one of the problems that’s causing this is that i is not 0, which is why the whole thing doesn’t work.

Yeah actually, that’s the main reason. You should move everything out of the for loop as you aren’t supposed to do this. Keep it in another scope, and make your own custom event using BindableEvents, which you can fire and then do everything there. Here.

local myCustomEvent = Instance.new("BindableEvent");
local message = script.Parent.Timer;
local players = game:GetService("Players");

game.ReplicatedStorage.Remotes.RemoteEvent.OnClientEvent:Connect(function()
    print("got");
    for i = 3, 0 do
        message.Text = i;
        print(i);
        wait(1);
        if print i == 0 then
            myCustomEvent:Fire();
            break
        end
    end
end)

myCustomEvent.Event:Connect(function()
    print("finished");
    local Chosen = players:GetPlayers()[math.random(#players:GetPlayers())];
    print(Chose.Name);
    wait(.1);
    local function typewrite(object, text, length)
        for i = 1, #text do
            object.Text = string.sub(text, 1, i);
            wait(length);
        end

        typewrite(message, "The Killer is: " .. Chosen.Name, 0.1);
        print("Done");
    end
end)

Hmm, I’m not sure if it’s yielded or not. It just doesn’t seem to run.

I’ll check. I’m not sure what you mean.

Edit: I got it to work by doing this instead. Hmm :thinking:

local messagetext = "The Killer is: " .. Chosen.Name
			
  for a = 1, #messagetext do
	  message.Text = string.sub(messagetext, 1, a)
	  wait(0.05)

That’s the point, that’s what I said earlier.

Nope none of the methods work. Both methods don’t even start the typwriting effect and there is no prints.

Also is it necessary to use "BindableEvent" and why do you do this ; after each line?

ah i see you haven’t been in the roblox development community for very long

Hmmhmm you were right. I accidentally replaced i with 1. I think I also misplaced this line:

typewrite(message, " The Killer is: " .. Chosen.Name, 0.1) 
	print("Done") -- This print doesn't run

Should have been 1 end lower. Thanks for you help :slightly_smiling_face:

Updated Code:

local function typewrite(object, text, length)
	for a = 1, #text, 1 do
		object.Text = string.sub(text, 1, a)
		wait(length)
		end
	end
	typewrite(message, "The Killer is: " .. Chosen.Name, 0.1)
	wait(1)
	typewrite(message, "Survivors avoid the killer... At all costs", 0.1)