How could I shorten this?

I want to shorten this line of code and make it shorter and simplified.

Input

	Status.Value = "You will be playing."
	
	task.wait(0.5)
	
	Status.Value = "You will be playing.."
	
	task.wait(0.5)
	
	Status.Value = "You will be playing on"..Chosen.."!"

Expected Output

You will be playing.

You will be playing..

You will be playing on MapName!
2 Likes

Try check these -

2 Likes

My bad for not giving context.

This won’t be for place teleporting, but for just a intermission text on the top of the screen

Is their any type of loop that I can use to add another period to the end of the text?

1 Like

Something like this might work for you. Change the variable TimesToLoop to better match how many times you want to dot to change:

local InitialString = "You will be playing"
local MapChosen = "MapName"
local TimesToLoop = 10
for i = 0, TimesToLoop - 1 do
	Status.Value = InitialString .. string.rep(".", i%3+1)
	wait(0.5)
end
Status.Value = string.format("You will be playing on %s!", MapChosen)
2 Likes
for i = 0, 9 do -- change 10 to how many times you want the dots to change.
   Status.Value = "You will be playing." .. string.rep(".", i % 3) -- Change the 3 to how many dots you want in total
   task.wait(.5)
end

Status.Value = "You will be playing on"..Chosen.."!"
1 Like

Thanks but, this won’t help me because Im not following or understanding on why I should do it this way, and what you changed and why.

1 Like

Here I will explain what the scripts the 2 people above me put.

for i=0,9 do — make a loop that repeated 10 times
    local dots = string.rep(".", i%3+1) -- This will repeat the dot a certain number of times, in this case the remainder of   i / 3 + 1
    local baseText =  "You will be playing."
    status.Value = baseText..dots -- Combine the 2 strings
    task.wait(0.5) -- Add a delay so players can see the animation (you may have to tweak this)
end

We can wrap that in a function as well if you will use this in multiple places in the code even with different string, something like this would work:

local function displayTextWithDots(baseText)
    for i=0,9 do — make a loop that repeated 10 times
        local dots = string.rep(".", i%3+1) -- This will repeat the dot a certain number of times, in this case the remainder of   i / 3 + 1
        status.Value = baseText..dots -- Combine the 2 strings
        task.wait(0.5) -- Add a delay so players can see the animation (you may have to tweak this)
    end
end

Then to call the function:

displayTextWithDots("You will be playing.")  -- I used the same text you used in the example
1 Like

Why would you divide i by three?

So you get a different number of .'s each time. For example, let’s say i is currently 0, i%3 = 0, so then we add 1 to get a minimum of 1, next iteration i=1 1%3=1+1=2 so now you’ll have 3 dot’s, understand?

1 Like

The reason is scalability. When you code you try to make it easy to be modified and adapted for when you inevitably expand upon previous development. You don’t know the length of time it will take to load so generating a proper loop will allow you to dynamically change how many “You will be playing on…” is printed.

With a loop you can easy interrupt/cancel a loop to stop it from displaying prints making a very easy modular piece of code that is reuseable in many places. (Module scripts on roblox)

1 Like

Does it round decimals?

Because 1/3 is 33.3% and + 1 is 1.333

And how would this give us 3?

This is a string, it creates the … at the end, it doesn’t do the loop it’s adding the dots to the end of the message.

This code is to Techincal and Tbh It would overwheml me to use this code…But it’s good that I can find a solution to his/her problems

But the math doesn’t make sense to me

If it’s the % symbol that’s causing the confusion I’ll try my best to elaborate on it a bit.

% is the modulus operator in Lua, and it returns the remainder after integer division between two numbers A and B

In simple terms, it just gets the remainder after diving A by B

In the code you can see that this is used in this line:

Status.Value = InitialString .. string.rep(".", i%3+1)

The line basically says: set Status.Value to InitialString (“You will be playing”) with i%3+1 dots after it. What does that mean?

Basically, let’s pretend were dividing i by 3. Remember that ‘i’ is the variable we’re using to loop over, so it takes values in between 0 and however many times you want it to loop (let’s say 10 times, so i takes values 0-9).

  • At first, i = 0, so how many times does 3 go into 0? 0 times. Is there anything left over when dividing by 3? Nope, so i%3 = 0 in this case
  • Let’s try i = 2 now, 3 goes into 2 a total of 0 times (still), but this time there is 2 left over when dividing by 3, so i%3 = 2.
  • Now when i = 3, 3 now finally goes into 3 (i) once. But now there is nothing left over, so i%3 = 0.
  • Likewise, when i = 4, i%3 = 1

You might be able to notice that performing i%3 only returns the values 0,1 or 2 depending on the remainder, restricting how high it can go. Thus, adding 1 to it will restrict it to the range 1-3.

When i = 0,1,2,3,4,5,6,7,8,9
i%3+1 = 1,2,3,1,2,3,1,2,3

Doing modulus with %3 is honestly pretty arbitrary, and I only chose it initially because it looks the nicest and it’s what you’ll see in most games. You can change it to %4 if you want up to 4 dots, %5 if you want up to 5 dots etc.

I hope this helps :slight_smile: but please ask if you have any other questions.

1 Like
local function Talk(Message, WaitTime)
   for _, TextToSay in pairs(Message) do
      Status.Value = TextToSay
      task.wait(WaitTime)
   end
end

Talk({
   "You will be playing.",
   "You will be playing..",
   "You will be playing on "..Chosen.."!",
}, 0.5)

This would make it a lot shorter if you had multiple Talk functions.

2 Likes

There are a lot of ways to make this code super deluxe and fancy, but the reality is that the code you started with is good enough!

You should keep it simple and keep it stupid, even if you’re pro at scripting.

If the intermission text at the top does the same thing every single time and there’s only one or two texts like it, then there’s no reason to make it overly flexible or put it in a function.