How would I create a random Weapon Spawning System?

I have parts around the map called WeaponSpawn, and I’m trying to make ever single child of the WeaponSpawns folder to randomly choose a Tool from the ServerStorage and Clone then parent it to the part. How would I do so? This is a simple script I’ve formed at the moment but it seems to not be working :thinking:

image
image

-- Variables
local weapons = game.ServerStorage.Weapons
local weaponsChildren = weapons:GetChildren()
local weaponCount = #weaponsChildren

local spawns = game.Workspace.GamePlay.WeaponSpawns
local spawnsChildren = spawns:GetChildren()
local spawnsAmount = #spawnsChildren

local spawnTime = math.random(30, 40)

local function spawnWeapon(player)

	-- Running through all parts in the folder
	for index, child in pairs(spawns:GetChildren()) do
		if child:IsA("BasePart") and child.Name == "WeaponSpawn" then -- Checking to make sure they are an actual weapon spawn
			
			-- Setting Properties
			child.Transparency = 1
			child.CanCollide = false
			
			-- Spawn Weapon
			local chooseWeapon = math.random(1, #weaponsChildren)
			print(chooseWeapon)
			--print("Weapons: "..weaponCount)
			if chooseWeapon == 1 then -- Glock
				local clone = weapons.Glock:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			elseif chooseWeapon == 2 then -- AK
				local clone = weapons.AK:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			elseif chooseWeapon == 3 then -- Scar
				local clone = weapons.Scar:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			elseif chooseWeapon == 4 then -- AWP
				local clone = weapons.AWP:Clone()
				clone.Parent = child
				clone:WaitForChild("Handle").Position = child.Position
			end
		end
	end
end
spawnWeapon()
1 Like

I am not exactly sure why it’s not working. However, I’ll give my observations on what seems odd. Perhaps it will change something:

  • Why does the function spawnWeapon() have an argument named ‘player?’

  • I can’t see where the script is located based on the screenshot you gave. Is it in ServerStorage? If so, I’m pretty sure it won’t run unless you put it in ServerScriptService;

  • You set the parent of the weapon to the part before changing its position. It’s generally a good idea to make sure the weapon is in the right spot before rendering it in the world;

  • I haven’t worked with tools in a long time, but is the only object in the tool the handle? If not, you might be only teleporting the handle, which could cause problems;

  • Make sure the weapon spawners are anchored.

May I also make a suggestion?
You create a random number based on the number of weapons in ServerStorage, but you compare said number to a constant set of items (namely, the Glock, AK, Scar and AWP). I would change the list to be more versatile and less code redundant. Like so:

local weaponList = {}
for _, weapon in pairs(weaponsChildren) do
	table.insert(weaponList, weapon)
end
...
local weaponChosen = weaponList[chooseWeapon]
--Make sure the indexed weapon actually exists
if weaponChosen then
	local newWeapon = weaponList[chooseWeapon]:Clone()
	...
end
...

Hope this helped!

1 Like

I’m pretty sure it’s not working because you have an argument for spawnWeapon called “player” but you never put a parameter when you called the function. You also don’t need this function (I think) so if you just delete the player argument it should work.

Hope this helps,
-dodle :grinning:

1 Like

First, I’d make sure that all the parts are properly welded to the Handle. I would also CFrame the object to the weapon spawns CFrame because I don’t think welded parts exactily work well with Position.

Also, instead of manually checking each weapon you could do

local part = weaponsFolder:GetChildren[math.random(1,#weapons)]

1 Like

This line doesnt work :sweat_smile: image

whoops i made a typo. You want to close the GetChildren with () before the brackets []. You also need to put the number of weapons in the math.random, or you can just do #weaponsFolder:GetChildren(). Forgot to put that also

Still seems to be an error, very confused here :joy:image

Dont know the technical reason, but you need to close GetChildren() with () at the end or else it will error, and then by putting the [math random] after it it gets that instance inside of the table. You also want to change weaponsChildren to

#weaponsFolder:GetChildren()

Instead of using conditionals for checking the weapon, why not just find the weapon through weaponschildren via index and then afterwards parent it accordingly?

for index, child in pairs(spawns:GetChildren()) do
   if child:IsA("BasePart") and child.Name == "WeaponSpawn" then
      child.Transparency = 1
      child.CanCollide = false

      local randomWeapon = weaponsChildren[math.random(1, #weaponsChildren)]:Clone()
      randomWeapon.Parent = child
      randomWeapon:WaitForChild("Handle").Position = child.Position
   end 
end

Thank you so much for your help definitly gonna add this, could you have a look at my recent post about this weapon spawning problem? Thank you so much :blush: Random Weapon Spawning Script only Spawning 1 Weapon

Thank you so much for your help definitely gonna add this, could you have a look at my recent post about this weapon spawning problem? Thank you so much :blush: Random Weapon Spawning Script only Spawning 1 Weapon

1 Like

its spawning only one weapon because you’re calling the function only once at the last line. You should create a loop that fires spawnWeapon().

I’ve solved this in my recent post :smiley:: Random Weapon Spawning Script only Spawning 1 Weapon - #14 by EmilyBendsSpace

1 Like

Yes I solved this 8 months ago