Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way in Roblox Studio to assign something a random number?

Asked by 7 years ago

So like I am trying to assign a folder a number value along with a model. What I mean by that is -

Say there is 1 small shack in a folder then it would be named small shack1 and inside replicated storage inside a folder there would be a folder named small shack1, how would I make them count up.

So small shack2 when a second shack is added?

I cant have the same number twice so it has to go up forever. I did the previously with my bags you drop on death, they all have a unique number but the houses are different so I am trying to figure out how I would go about doing this?

Any ideas or anything really? I know this question is like really confusing but I am basically just trying to assign things a unique number honestly.

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

This is the best I can do with the little information you game me. For it to work the object must be named "small shack" and be parented to the folder containing the models.

01local c1 = 1 -- Folder Count
02local c2 = 1 -- Replicated Storage Count
03local folder1= script.Parent -- The Folder containing Models
04local folder2 = game:GetService("ReplicatedStorage").FolderNameHere -- The Replicated Storage folder, replace FolderNameHere With correct folder
05 
06for _,child in ipairs(folder1) do -- Loop through the folder containing models
07    if child.Name == "small shack" then -- Detect and check model name
08        child.Name = "small shack" .. tostring(c1 -- Rename the model
09        c1 = c1 + 1 -- Increase the number by 1 for next time
10    end
11end
12for _,child in ipairs(folder2) do -- Loop through the Replicated Storage folder
13    if child.Name == "small shack" then -- Detect and check model name
14        child.Name = "small shack" .. tostring(c2)-- Rename the model
15        c2= c2+ 1 -- Increase the number by 1 for next time
View all 30 lines...

Next time please include more information. I hope this fixed your problem!

Ad
Log in to vote
1
Answered by 7 years ago

You're not looking for a random number but more so a unique number.

Here's a small example to illustrate the idea. Here I use a string table and string.find(string, searchString). If the searchString cannot be found it will return nil. You'll also need to use tostring() as shown below since you'll be using objects and not strings.

You could also have a continuous counter stored after each iteration and saved with DataStore but that may be more of a hassle.

01local numOfShacks = 0
02local SMALL_SHACK = "Small_Shack"
03local shacks = {"Small_Shack1","Small_Shack2","Small_Shack3","Small_Shack4","Small_Shack5"}
04 
05local function CountShack()
06    numOfShacks = 0
07    for _,shack in pairs(shacks) do
08        if string.find(tostring(shack), SMALL_SHACK) then
09            print(shack)
10            numOfShacks = numOfShacks + 1
11        end
12    end
13end
14 
15local function MakeNewShack()
View all 25 lines...
0
Nice answer! TdaGreat04 76 — 7y

Answer this question