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.
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.
01 | local c 1 = 1 -- Folder Count |
02 | local c 2 = 1 -- Replicated Storage Count |
03 | local folder 1 = script.Parent -- The Folder containing Models |
04 | local folder 2 = game:GetService( "ReplicatedStorage" ).FolderNameHere -- The Replicated Storage folder, replace FolderNameHere With correct folder |
05 |
06 | for _,child in ipairs (folder 1 ) 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 (c 1 -- Rename the model |
09 | c 1 = c 1 + 1 -- Increase the number by 1 for next time |
10 | end |
11 | end |
12 | for _,child in ipairs (folder 2 ) 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 (c 2 ) -- Rename the model |
15 | c 2 = c 2 + 1 -- Increase the number by 1 for next time |
Next time please include more information. I hope this fixed your problem!
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.
01 | local numOfShacks = 0 |
02 | local SMALL_SHACK = "Small_Shack" |
03 | local shacks = { "Small_Shack1" , "Small_Shack2" , "Small_Shack3" , "Small_Shack4" , "Small_Shack5" } |
04 |
05 | local 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 |
13 | end |
14 |
15 | local function MakeNewShack() |