How do I fix this randomizing loot generation?

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.

Any help is appreciated!

3 Likes

Are you calling the module script from another script? That may be the issue

2 Likes

If you didn’t use require() from another script this won’t run. Try making another script and just require this script.

Also, if this is the full module code, you didn’t define what is:

1 Like

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.

1 Like

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)."

1 Like

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?

sorry if it’s confusing lol

1 Like

return outputs the result of the function RandomizeDecoration()

To go back to the function you don’t need to use return, simply call the function.

2 Likes

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.

1 Like

Oh I see, but I tried that too, and it’s just doing the same thing…

1 Like

I kind of see what your saying, but I’m not sure how to incorporate that into the script? Could you show like an example if you could?

1 Like

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

1 Like
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()?

2 Likes

Yeah I noticed that, but i fixed it before I had to require the module so it wasn’t a problem

2 Likes

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)

not sure if that makes sense

1 Like

In the random function you can use repeat — until it is a LootSpot4

2 Likes

Oh wait it works perfectly now, I got rid of the randomize function and just replaced basically the copy and paste parts with a repeat!!

thanks!

2 Likes

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