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())
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
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())
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()
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 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
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