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

Retrieving a word with numbers from another script?

This is all I want to know..

If i say:

script.Parent.Name = "Hello".. math.random(1,100)

then how will i use :FindFirstChild to get the random number?

example:

local count = math.random(1,100)
local objectOne = Instance.new("IntValue",workspace)
local objectOne.Value = "Brick"..count
count=count + math.random(1,100)
local objectTwo = Instance.new("IntValue",workspace)
objectTwo.Value = "Brick"..count

then in another script

print(workspace:FindFirstChild("Brick"..math.random))

How would this work?

2 comments

0

First of all , are you trying to store a string data type inside of instance "IntValue" , which has an integer data type?

0

What are you trying to get from the workspace? Maybe you should change the " workspace:find... " to "workspace.IntValueName"

2 answers

✓ Accepted answer

There are a couple strategies you can use:

  1. Iterate over all the children in the workspace and use pattern matching, as F4ULT1NTH3D4T4 suggests (I don't recommend this; why iterate over it manually if you don't have to?).
  2. Merge it into a single script (so that the same script handles renaming the object). I recommend this method.
  3. Use an ObjectValue, BindableFunction, or ModuleScript to transmit a reference to the object from one script to the other. (The script doing the renaming might add the Part to a list stored in the ModuleScript, for instance, or else it might add an ObjectValue to, say, workspace.RenamedParts (some folder that the main script knows about).)

You could have a ModuleScript whose sole purpose is to return a table that other scripts can share:

--Module script (let's call it "List" and put it in ServerScriptService)
return {}

--Script that does renaming
script.Parent.Name = "Hello".. math.random(1,100)
local list = require(game.ServerScriptService.List)
table.insert(list, script.Parent)

--Script that wants the list of items:
local list = require(game.ServerScriptService.List)
--do whatever you want to 'list'. Note that you cannot say "list = {}", but you can manually delete all the entries, if needed. ex:
local obj
for i = 1, #list do
	obj = list[i]
	--deal with obj here
	list[i] = nil -- remove the object from the list so we don't deal with it ever again in the future. We mustn't use table.remove or else that'll mess up the rest of the for loop.
end

1 comment

0

Yes, this is exactly what I needed, thank you chess! You are awesome :D

http://wiki.roblox.com/index.php?title=Global_namespace/String_manipulation#Patterns

Number = ("Brick" .. count):match("%d+") --returns the count

4 comments

0

what does %d+ mean?

0

how will i use it from a different script?

0

%d is any digit in a string called a pattern. If you don't know how many digits there are in a string then you put a + right after.

0

This is only a partial solution; you would need to iterate over all the children in workspace and see if their name matches the desired pattern (which should be "Brick(%d+)", to find only something named "Brick" and then to return the number attached to it).

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