How to use a number for an instances name

I created a roblox game, and I made a tool that you can turn in to a NPC to get gold. I want their to be 5 tools on the island, and once they are all
turned in 5-10 more spawn. I placed parts where I want them to have a chance of spawning. Their are 10 possible spawns for 5 tool clones. How do I make each tool go to a random spawn that DOES NOT already have a tool on it? Below is my script but it does not work.

The issue is that in line
wood.CFrame = game.Workspace.Spawns:FindFirstChild(Table[i]).CFrame

Table[i] is a number and not an instance, even though all of the spawns names are numbers.

So, How do I fix this? Any and All help is appreciated.

local function spawnWood()
	for i = 0, 5, 1 do
		local Spawns = game.Workspace.MaterialSpawns:GetChildren()
		local Table = {}
		--creates a table of 5 random different numbers between 1 and 10
		for i = 1, 5 do
			local Rand = math.random(1, 10)
			for i, v in pairs(Table) do
				if v == Rand then
					repeat
						Rand = math.random(1, 10)
					until v ~= Rand
				end
			end
			table.insert(Table, Rand)
		end
	end
		--now attempt to make the wood go to the correct spawns
	for i = 0, 5, 1 do	
		local wood = game.ReplicatedStorage.Wood:Clone()
		wood.Parent = workspace
			--set woods cframe to workspace.Spawns child i
			wood.CFrame = game.Workspace.Spawns:FindFirstChild(Table[i]).CFrame
	end
end


game.ReplicatedStorage.Level1.OnServerEvent:Connect(spawnWood())

Assuming you want to change the name of the Wood:

wood.Name = i --changes the name to the current index

sorry I was not very clear, I want the wood to go to say game.Workspace.Materials.5 what it is recieving seems to be something like game.Workspace.Materials.“5” which is not an instance I guess

I want each wood tool to go to the position of part. each parts name is a number from 1-10 as seen in the screenshot. its just not working

1 Like

The variable Table is not in the correct scope. You can fix this by moving the defining line to a more general scope:

local function spawnWood()
        local Table = {} -- move the defining line here instead so both for loops can "read the variable"
	for i = 0, 5, 1 do
		local Spawns = game.Workspace.MaterialSpawns:GetChildren()
		for i = 1, 5 do
			local Rand = math.random(1, 10)
			for i, v in pairs(Table) do
				if v == Rand then
					repeat
						Rand = math.random(1, 10)
					until v ~= Rand
				end
			end
			table.insert(Table, Rand)
		end
	end
		--now attempt to make the wood go to the correct spawns
	for i = 0, 5, 1 do	
		local wood = game.ReplicatedStorage.Wood:Clone()
		wood.Parent = workspace
			--set woods cframe to workspace.Spawns child i
			wood.CFrame = game.Workspace.Spawns:FindFirstChild(Table[i]).CFrame
	end
end

game.ReplicatedStorage.Level1.OnServerEvent:Connect(spawnWood())
1 Like

Nothing happened, no errors though, just nothing happened…
It did not clone

You could reference it like:

workspace.Materials[“5”]
-> Doesn’t error

Instead of:

workspace.Materials.5
-> Errors

When you are connecting to a remote event listener, the function doesn’t need the parenthesis

Incorrect:

game.ReplicatedStorage.Level1.OnServerEvent:Connect(spawnWood())

Correct:

game.ReplicatedStorage.Level1.OnServerEvent:Connect(spawnWood)

oooh okay that actually makes a lot of sense, i will try it now

Technically, you are correct, but this is unnecessary.

Luau (Roblox Lua) will automatically concatenate numbers into strings if the script requires one.

1 Like

Didn’t know that. I’ll go ahead and test that out real quick.

Edit: still getting an error

Ah, well it seems I have been defeated. I never made scripts in that format before. I always did something like part1 or part2

1 Like

the output says the function is running but I’m not seeing anything.
I did take off the remote event and replaced it with a normal function for faster testing, would that have messed it up?


local function spawnWood()
	local Table = {} -- move the defining line here instead so both for loops can "read the variable"
	for i = 0, 5, 1 do
		local Spawns = game.Workspace.MaterialSpawns:GetChildren()
		for i = 1, 5 do
			local Rand = math.random(1, 10)
			for i, v in pairs(Table) do
				if v == Rand then
					repeat
						Rand = math.random(1, 10)
					until v ~= Rand
				end
			end
			table.insert(Table, Rand)
		end
	end
	--now attempt to make the wood go to the correct spawns
	for i = 0, 5, 1 do	
		local wood = game.ReplicatedStorage:WaitForChild("Wood"):Clone()
		wood.Parent = workspace
		--set woods cframe to workspace.Spawns child i
		wood.CFrame = game.Workspace.Spawns:FindFirstChild(Table[i]).CFrame
	end
end

spawnWood()
1 Like

Did you by any chance set the CFrame of the parts in the Spawn Folder in studio yet?
If not, it could be that the script is working, but at the wrong position.

Edit: No, running the function without the remote event would cause fatal damage do the script unless it is dependent on arguments, which this script does not have any. I still suggest putting it back in case you forget as it doesn’t matter

I did set the position I think. They are highlighted in this screenshot

I got “Error, Argument 1 missing or nil” on line 22
The wood is spawning on its position in replicated storage and not on the spawns positions. It is also only spawning 1 of the wood clones

I think I see it. It has to do something with this line. Unfortunately, I am out of time to help. I wish you the best of luck.

1 Like

It has something to do with Table[i] also I am pretty sure you cannot set the CFrame of a tool but must instead set the position

Yes, try setting the handle of the tool’s CFrame. Like this:

wood.handle.CFrame = game.Workspace.Spawns:FindFirstChild(Table[i]).CFrame

AHA! I got it! It now works. The issue was CFrame.new was not used and the for i = 0 was returning item 0 of table so that also broke the script.

working script

	local Table = {} -- move the defining line here instead so both for loops can "read the variable"
	for i = 0, 5, 1 do
		local Spawns = game.Workspace.MaterialSpawns:GetChildren()
		for i = 1, 5 do
			local Rand = math.random(1, 10)
			for i, v in pairs(Table) do
				if v == Rand then
					repeat
						Rand = math.random(1, 10)
					until v ~= Rand
				end
			end
			table.insert(Table, Rand)
		end
	end
	--now attempt to make the wood go to the correct spawns
	for i = 1, 5, 1 do	
		local wood = game.ReplicatedStorage:WaitForChild("Wood"):Clone()
		wood.Parent = workspace
		--set woods cframe to workspace.Spawns child i
		--can you set the Cframe of a tool?
		--no you can't
		--so set the position of the wood to the position of the part
		wood.Handle.CFrame = CFrame.new(game.Workspace.MaterialSpawns:FindFirstChild(Table[i]).Position)
	end
end

spawnWood()
script.Enabled = false
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.