Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

local c1 = 1 -- Folder Count
local c2 = 1 -- Replicated Storage Count
local folder1= script.Parent -- The Folder containing Models
local folder2 = game:GetService("ReplicatedStorage").FolderNameHere -- The Replicated Storage folder, replace FolderNameHere With correct folder

for _,child in ipairs(folder1) do -- Loop through the folder containing models
	if child.Name == "small shack" then -- Detect and check model name
		child.Name = "small shack" .. tostring(c1 -- Rename the model
		c1 = c1 + 1 -- Increase the number by 1 for next time
	end
end
for _,child in ipairs(folder2) do -- Loop through the Replicated Storage folder
	if child.Name == "small shack" then -- Detect and check model name
		child.Name = "small shack" .. tostring(c2)-- Rename the model
		c2= c2+ 1 -- Increase the number by 1 for next time
	end
end

folder1.ChildAdded:connect(function(child) -- See if child added to folder1
	if child.Name = "small shack" then -- Detect and check model name
		child.Name = "small shack" .. tostring(c1) -- Rename model
		c1 = c1 + 1 -- Increase the number by 1 for next time
	end
end)
folder2.ChildAdded:connect(function(child) -- See if child added to folder2
	if child.Name = "small shack" then -- Detect and check model name
		child.Name = "small shack" .. tostring(c2) -- Rename Model
		c2= c2+ 1 -- Increase the number by 1 for next time
	end
end)

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.

local numOfShacks = 0
local SMALL_SHACK = "Small_Shack"
local shacks = {"Small_Shack1","Small_Shack2","Small_Shack3","Small_Shack4","Small_Shack5"}

local function CountShack()
	numOfShacks = 0
	for _,shack in pairs(shacks) do
		if string.find(tostring(shack), SMALL_SHACK) then
			print(shack)
			numOfShacks = numOfShacks + 1
		end
	end
end

local function MakeNewShack()
	local newShackName = SMALL_SHACK .. numOfShacks + 1
	
	table.insert(shacks,newShackName)
end

CountShack()
print(numOfShacks) --5
MakeNewShack()
CountShack()
print(numOfShacks) --6

1 comment

0

Nice answer!

This discussion is preserved as it was. New answers, comments, and votes are closed.