This is what I’m trying to achieve:
Random “rooms” generate and they contain different types of possible decorations/loot to spawn
--Module
local DecorHandlerModule = {}
PossibleDecorSpots.ChildAdded:Connect(function(child)
DecorHandlerModule.PickDecor(child)
end)
DecorHandlerModule.PickDecor = function(spot)
local DecorationInUse
local function RandomizeDecoration()
local decorations = RSDecorationStorage:GetChildren()
local randomDecor = decorations[math.random(1, #decorations)]
DecorationInUse = randomDecor
end
if spot:GetAttribute("SpotType") == "LootSpot4" then
RandomizeDecoration()
if DecorationInUse:GetAttribute("DecorType") == "LootSpot4" then
local decorClone = DecorationInUse:Clone()
decorClone.Parent = SetDecorations
decorClone:PivotTo()
if decorClone:FindFirstChild("Loot") then
for i, v in pairs(decorClone.Loot:GetChildren()) do
v.Parent = PossibleLootSpots
end
end
spot:Destroy()
else
return RandomizeDecoration()
end
end
end
return DecorHandlerModule
This is my script for how the decorations will spawn.
As of right now, the script isn’t even running, nor is it giving an error anywhere. (not in output or inside the script itself)
I want to try to fix and optimize this script, since the if DecorationInUse:GetAttribute("DecorType") == "LootSpot4" then part is going to be copy and pasted several times since there are going to be for example, LootSpot1, LootSpot2, LootSpot3, etc. in the workspace.
I know it’s a little confusing with the context I am showing but please ask questions if you need answers.
I’m using the PossibleDecorSpots.ChildAdded:Connect(function(child) to start the functions? I’m not sure if this is how you do it since it seems to not be running.
The ModuleScript is still not going to run. The code won’t execute until you require it.
"By itself, a module script can’t run code — it needs to be loaded in another script using the keyword require(). The function require() accepts one argument, the location of the module script in the Explorer.
To use a module, in a separate script, set a variable equal to require(moduleScript)."
Oh wow I didn’t know that, but now I did require it and it kind of works…
The decorClone’s are not filling up each spot. It’s just that every so often the decorClone is pivoted to the spot’s cframe.
Is this because the return RandomizeDecoration() is not how you return BACK to the function and restart it to try and get the right DecorationInUse?
or because it’s running too fast how the .ChildAdded:Connect() to run?
I think you are trying to make 4 clones each time right?
Make a for loop to iterate through all available spots and make a new unique clone for each spot inside the loop.
:PivotTo() requires an argument to move the pivot to, which may also be the source of the problem you were having where the clones aren’t filling up the spots.
(NVM)
if DecorationInUse:GetAttribute("DecorType") == "LootSpot4" then
for i,v in spots do
local decorClone = DecorationInUse:Clone()
decorClone:PivotTo(CFrame)
v.Parent = PossibleLootSpots
end
end
I hope that’s what you are looking for. I’m still not sure why are you returning RandomizeDecoration()?
I’m returning the RandomizeDecoration() because I have certain models that can fit the specific spot or the "LootSpot4 attribute. So if the randomization gets it “wrong” then I would call back to it to try and get the right one instead.
(i trying to do this so I don’t have to make a stupid amount of folders)