How to fix this?

Hello robloxians,
so i try to make a random text per 5 sec to someting inside my game and is no works… can sombody tell me why is no works?

Heres the code:
Res = {
Res1 = “I AM LIKE THIS GAME!.”;
Res2 = “LETS PLAY MORE.”;
Res3 = “Zzz”;
Res4 = “Lets Eat Someting…”;

}

while wait(5) do
script.Parent.Text = Random(Res)
end

And the Problem:

You cant index a Dictionary with a number. Instead first you have to remove ALL the keys in the dictionary as they are all just Res1, Res2, Res3.

Both methods will work, choose which one better fits your situation

With the Keys

Res = {
     Res1 = “I AM LIKE THIS GAME!.”;
     Res2 = “LETS PLAY MORE.”;
     Res3 = “Zzz”;
     Res4 = “Lets Eat Someting…”;
 }   

 while wait(5) do
      script.Parent.Text = Res["Res"..math.random(1, 4)] --//You cant use #Dict to get the number of items in a dictionary 
 end

Without The Keys

     Res = {
       “I AM LIKE THIS GAME!.”;
        “LETS PLAY MORE.”;
        “Zzz”;
        “Lets Eat Someting…”;
    }

    while wait(5) do
         script.Parent.Text = Res[math.random(1, #Res)]
    end
3 Likes

Quick sidenote: use backticks ` to format your code, the key is usually left of 1 or underneath esc on your keyboard.

`inline code` = inline code
```lua
-- code here
```

-- code here

The function you’re looking for is math.random. This function takes a variety of arguments, in which I’ll explain:

  • math.random() on its own generates a pseudo-random real number between 0 and 1. Like 0.57153
  • math.random(max) generates a random integer (no decimals) between 1 and max, inclusively.
  • math.random(min, max) generates a random integer between min and max, inclusively.

The 2nd implementation is what you might wanna use, but the 3rd is typically used for readability sake. Ex: math.random(1, max)

2 Likes

thanks @LinxyDotLua, is works!. :+1: :slight_smile:

3 Likes