Random Item Spawning Problems

I’m trying to spawn items grabbed from replicated storage in random spots on my map

The script isn’t working any ideas on how to fix it

The script is in serverscriptservice

-- Variables
local Items = game.ServerStorage.Items
local ItemsSpawns = game.Workspace.ItemSpawns:GetChildren()
local ItemsSpawnsAmount = #ItemsSpawns

local ItemsChildren = Items:GetChildren()
local ItemCount = #ItemsChildren

-- Loop through each item spawn
for i,v in pairs(ItemsSpawns) do
	-- Select a random item from the available ones
	local randomItem = ItemsChildren[math.random(ItemCount)]:Clone()
	-- Print the selected item
	print(randomItem)
	-- Place the item at the spawn point
	randomItem.Parent = v
	randomItem.Handle.CFrame = v.CFrame
	-- Print success message
	print("success")
end

-- Get children of items again (retrieving fresh list of items)
local Items = game.ServerStorage.Items
local ItemsChildren = Items:GetChildren()
local ItemCount = #ItemsChildren

-- Get children of ItemSpawns in Workspace
local spawns = game.Workspace.ItemSpawns
local spawnsChildren = spawns:GetChildren()
local spawnsAmount = #spawnsChildren

-- Set random spawn time between 30 and 40 seconds
local spawnTime = math.random(30, 40)

-- Define function to spawn items
local function spawnItem(player)
	-- Loop through each child in ItemSpawns
	for index, child in pairs(spawns:GetChildren()) do
		-- Check if the child is a BasePart and named "ItemSpawn"
		if child:IsA("BasePart") and child.Name == "ItemSpawn" then
			-- Make the spawn point transparent and non-collidable
			child.Transparency = 1
			child.CanCollide = false
			-- Select a random item
			local chooseItem = math.random(1, #ItemsChildren)
			print(chooseItem)
			-- Spawn the selected item based on the random choice
			if chooseItem == 1 then
				local clone = Items.ClassicSword:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			elseif chooseItem == 2 then
				local clone = Items.AK:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			elseif chooseItem == 3 then
				local clone = Items.Scar:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			elseif chooseItem == 4 then
				local clone = Items.AWP:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			end
		end
	end
end

-- Call the spawnItem function
spawnItem()

1 Like

It seems like you are only calling the function once, so I decided to redo the code, if this is how you would like it to work, then great, but if I messed something up let me know:

-- Folders
local Items = game.ServerStorage.Items
local ItemsSpawns = game.Workspace.ItemSpawns
local itemSpawnLocation = game.Workspace.ItemLocations

-- Item Children
local ItemsChildren = Items:GetChildren()
local ItemCount = #ItemsChildren

-- Item Locations
local itemLocations = #itemSpawnLocation:GetChildren()

-- Random Variables
local randomTime
local randomLocation
local randomTool

-- Finds the location of where to put the part
local function findSpawnPlace()
	local itemLocation = math.random(1, itemLocations) -- Gets the random number from 1 to the amount of item locations there are (Parts)
	
	return itemSpawnLocation:FindFirstChild(itemLocation) -- Returns the Item Location, (Name the parts in number: 1, 2..)
end

-- Makes setting up the tools quicker
local function setUpClonedTool(tool: Tool, itemLocation: CFrame)
	tool.Parent = ItemsSpawns -- Sets the tools parent to the item Spawns
	tool:PivotTo(itemLocation) -- Moes the tool to the spawn location
end

-- Finds the location and finds a random number to send to another function to process it
local function spawnItem(location: BasePart)
	local randomInt = math.random(1, ItemCount) -- Finds a random number from 1 to the number of items
	
	if randomInt == 1 then
		setUpClonedTool(Items.ClassicSword:Clone(), location.CFrame)
	elseif randomInt == 2 then
		setUpClonedTool(Items.AK:Clone(), location.CFrame)
	elseif randomInt == 3 then
		setUpClonedTool(Items.Scar:Clone(), location.CFrame)
	elseif randomInt == 4 then
		setUpClonedTool(Items.AWP:Clone(), location.CFrame)
	end
end

-- Makes sure to continue to load the function
while true do
	randomTime = math.random(30, 40)
	task.wait(randomTime)
	randomLocation = findSpawnPlace()
	spawnItem(randomLocation)
end
1 Like

There is no folder called itemspawnlocations the item spawners are in the itemspawns

Ok, so if you don’t want to add an item spawners folder, where do you want them to go, because I used Item spawn as where the items are stored until they are collect. If you still want to you can make a folder called ‘ItemLocations’ then put parts in there that show where they spawn, if not where on the map are they going? Are they using parts to teleport the tool to, or what?

Screenshot 2024-03-02 135536

this is the folder

Okay so itemspawns is the folder with the spawnlocations called ItemSpawners(they are parts) and the items themselves are in a folder inside of replicated storage

Yes, I understand that there is a folder, what is in the folder?

Screenshot 2024-03-02 140110
Screenshot 2024-03-02 140133

Ok, understood, now where are these items going after they are spawned? To a players inventory, to a folder in the workspace, in the workspace without a folder?

I actually didn’t think about that I want the items to spawn in the location of the itemspawners in a folder called SpawnedItems

Ok, so now all you need to do is inside of the ItemSpawns folder is change each of the names to 1, 2, 3, and 4. It really doesn’t matter what one, but here is your updated code:

-- Folders
local Items = game.ServerStorage.Items
local ItemsSpawns = game.Workspace.ItemSpawns
local SpawnedItems = game.Workspace.SpawnedItems

-- Item Children
local ItemsChildren = Items:GetChildren()
local ItemCount = #ItemsChildren

-- Item Locations
local itemLocations = #ItemsSpawns:GetChildren()

-- Random Variables
local randomTime
local randomLocation
local randomTool

-- Finds the location of where to put the part
local function findSpawnPlace()
	local itemLocation = math.random(1, itemLocations) -- Gets the random number from 1 to the amount of item locations there are (Parts)
	
	return ItemsSpawns:FindFirstChild(itemLocation) -- Returns the Item Location, (Name the parts in number: 1, 2..)
end

-- Makes setting up the tools quicker
local function setUpClonedTool(tool: Tool, itemLocation: CFrame)
	tool.Parent = SpawnedItems -- Sets the tools parent to the item Spawns
	tool:PivotTo(itemLocation) -- Moes the tool to the spawn location
end

-- Finds the location and finds a random number to send to another function to process it
local function spawnItem(location: BasePart)
	local randomInt = math.random(1, ItemCount) -- Finds a random number from 1 to the number of items
	
	if randomInt == 1 then
		setUpClonedTool(Items.ClassicSword:Clone(), location.CFrame)
	elseif randomInt == 2 then
		setUpClonedTool(Items.AK:Clone(), location.CFrame)
	elseif randomInt == 3 then
		setUpClonedTool(Items.Scar:Clone(), location.CFrame)
	elseif randomInt == 4 then
		setUpClonedTool(Items.AWP:Clone(), location.CFrame)
	end
end

-- Makes sure to continue to load the function
while true do
	randomTime = math.random(30, 40)
	task.wait(randomTime)
	randomLocation = findSpawnPlace()
	spawnItem(randomLocation)
end

Let me know if it works

you put the Items forlder in serverstorage?? because you said you were grabbing itfrom replicated storage

do you know what this output is?

Did you change the parts name inside of the ItemSpawner to 1,2,3, and 4?

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