Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why won't this loot spawn system work?

lighting = game:GetService("Lighting")
assets = lighting.Assets
items = assets.Items
loot = {}
for _, child in pairs(workspace.Loot:GetChildren()) do
    table.insert(loot, child.Name)
end


common = {
	"Part"
}

uncommon = {
	"CornerWedge"
}

rare = {
	"Truss"
}

legendary = {
	"Wedge"
}

exotic = {
	"WedgeLarge"
}

function spawnloot()
	for _, v in pairs(loot) do
    	local item
   		if v.Name == "Common" then
       		item = common[math.random(#common)]		
		elseif v.Name == "Uncommon" then
        	item = uncommon[math.random(#uncommon)]
		elseif v.Name == "Rare" then
        	item = rare[math.random(#rare)]
		elseif v.Name == "Legendary" then
        	item = legendary[math.random(#legendary)]
		elseif v.Name == "Exotic" then
        	item = exotic[math.random(#exotic)]
    	end

		if item then
        	local randomloot = items[item]:Clone()
        	randomloot.Parent = game.Workspace
    		randomloot.CFrame = v.CFrame
		end
	end
end

while true do
	wait(1) --360 seconds; 6 minutes
	spawnloot()
end



Why won't the loot spawn? Thanks!

1 answer

spawnloot should probably take loot as a parameter, instead of it being a global variable. This also makes it easy to document your code and see what the problem is.

-- loot: a list of strings
function spawnloot(loot)
	-- ......

Notice that loot is not a list of strings -- it's a list of instances.

Either you have to change loot to be the strings in the thing:

local loot = {}
for _, child in pairs(workspace.Loot:GetChildren()) do
	table.insert(loot, child.Name)
	 -- or `child.Value` if that's what you meant
end

or you have to change your spawnloot function

-- inside function...
	if v.Name == "Common" then

Since you want to keep the .Position, probably, the second option is probably better.

Cleanup

First, the 1 in math.random is implied, you can just use common[math.random(#common)].

Second, you're repeating yourself a lot. Do it like this, instead:

for _, v in pairs(loot) do
	local item
	if v.Name == "Common" then
		item = common[math.random(#common)]
	elseif v.Name == "Uncommon" then
		item = uncommon[math.random(#uncommon)]
	end
	
	if item then
		local randomloot = items[item]:Clone()
		randomloot.Parent = workspace
		randomloot.CFrame = v.CFrame
	end
end

Also, as long as the lookups are simple, you can just use a bigger table to avoid a bunch of elseifs.

qualities = {
	Common = {"Part"},
	Uncommon = {"CornerWedge"},
	-- etc
}

......
	for _, v in pairs(loot) do
		local list = qualities[v.Name]
		if list then
			local item = list[math.random(#list)]
			local randomloot = items[item]:Clone()
			......

2 comments

0

I updated my script, and it still doesnt work, but I have no errors still??

0

... Read what I said, carefully. Read WHY I said to make the changes I did. In particular, pay attention to what I just bolded.

This discussion is preserved as it was. New answers, comments, and votes are closed.