Hi!
I am trying to learn basic scripting and modules and have run into a problem.
Currently I’m making a Loot Spawn script and I need some guidance with it!
I found @epoke466 s code on a similar thread, but I don’t want the script to be inside every spawner.
local ReplicatedStorage = game:GetService("ReplicatedStorage") --ReplicatedStorage
local Loot = ReplicatedStorage.Loot --The folder containing all the loot
local Spawner = script.Parent
Spawner.Transparency = 1
function SpawnLoot() --This is a fuction, anything that happens between here and line 19 where it says end will happen any time SpawnLoot() is in the code. E.g. line 21
local LootGroup = Loot:FindFirstChild(script.Parent:GetAttribute("LootType")) --Get the right type of loot based off of the attribute
local LootTable = {} --Create a empty table
for i, Tool in pairs(LootGroup:GetChildren()) do --Fill the table with all the loot in the loot group
table.insert(LootTable, Tool)
end
local ChosenLoot = LootTable[math.random(1, #LootTable)] --Pick a random tool from the loot group
local Loot = ChosenLoot:Clone() --Make a copy of it
Loot.Parent = workspace --Put it in workspace
Loot.CFrame = script.Parent.CFrame --Move to to the position of the node
end
SpawnLoot()
The issue is that the script needs to be placed in every single spawner. I would like to create a single script that runs from ServerScriptService or similar.
Alternativley and/or for learning purposes this spawn script could be a modular function and be called from another script, e.g. when a timer resets.
I have tried and failed at doing it myself and would appreciate if someone explained to me what and where it goes wrong.
The approach you are trying to take is on the right path, but the code does not need to be modular.
To condense everything into one script, you need to locate all of the spawners. In this example, there would be a folder in workspace named “Spawners”:
local spawners = workspace.Spawners -- A folder with all the spawners
for _, spawner in spawners:GetChildren() do
-- spawn loot
end
You can use other means to get the spawners such as looking for them by name or by using CollectionService, but this will do for now.
You can pretty much put the code inside SpawnLoot() inside the for loop, and just replace the "script.Parent"s with “spawner”.
I don’t know whether or not what was inside the SpawnLoot function you provided was in full so I couldn’t implement that but hopefully this helps.
Hello!
Thanks for the reply!
I did not manage to fix it this evening, but I think I got a bit closer to solving it!
One of the spawners now spawns an item!
Here’s the current code as of today.
local ReplicatedStorage = game:GetService("ReplicatedStorage") --ReplicatedStorage
local Loot = ReplicatedStorage.Loot --The folder containing all the loot
local SpawnerFolder = workspace.SpawnerFolder -- A folder with all the spawners
for _, Spawner in SpawnerFolder:GetChildren() do -- Selects all children in SpawnerFolder
-- Both spawners are visible from the start
wait(5)
Spawner.Transparency = 1 -- Only one spawner gets affected by this. Why?
function SpawnLoot() -- Spawn Loot Function
local LootGroup = Loot:FindFirstChild(Spawner:GetAttribute("LootType")) -- Finds the correct Loot subfolder based on the Spawner attribute name
local LootTable = {} -- Create a empty table
for _, Tool in pairs(LootGroup:GetChildren()) do -- Fill the table with all the loot in the loot group
table.insert(LootTable, Tool)
end
local ChosenLoot = LootTable[math.random(1, #LootTable)] -- Picks a random object/loot from the loot group
local SpawnedLoot = ChosenLoot:Clone() -- Makes a copy of the chosen loot
SpawnedLoot.Parent = workspace -- Puts the spawned loot in workspace
SpawnedLoot.CFrame = Spawner.CFrame -- Move the spawned loot to the position of the Spawner
end
end
SpawnLoot()
Here’s the place with everything for testing, would be grateful if someone solved this!
put the function along with necessary variables inside a module script server storage and change the function name to
function module:SpawnLoot(spawner)
then call the fuction whenever you want to spawn the loot like
local m1=require(modulescript)
m1:SpawnLoot(spawner)
something like
ModuleScript
local module={}
local ReplicatedStorage = game:GetService("ReplicatedStorage") --ReplicatedStorage
local Loot = ReplicatedStorage.Loot --The folder containing all the loot
function module:SpawnLoot(Spawner) --This is a fuction, anything that happens between here and line 19 where it says end will happen any time SpawnLoot() is in the code. E.g. line 21
Spawner.Transparency = 1
local LootGroup = Loot:FindFirstChild(Spawner:GetAttribute("LootType")) --Get the right type of loot based off of the attribute
local LootTable = {} --Create a empty table
for i, Tool in pairs(LootGroup:GetChildren()) do --Fill the table with all the loot in the loot group
table.insert(LootTable, Tool)
end
local ChosenLoot = LootTable[math.random(1, #LootTable)] --Pick a random tool from the loot group
local Loot = ChosenLoot:Clone() --Make a copy of it
Loot.Parent = workspace --Put it in workspace
Loot.CFrame = Spawner.CFrame --Move to to the position of the node
end
return module