Random Weapon Spawning Script only Spawning 1 Weapon

Heya, I’m trying to make a random weapon spawning system. Its fairly easy this is the script I have right now,

-- Weapon Spawn Variables
local weapons = game.ServerStorage.Weapons
local weaponSpawns = game.Workspace.GamePlay.WeaponSpawns:GetChildren()
local weaponSpawnsAmount = #weaponSpawns

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

local spawnRate = 30

for index, child in pairs(weaponSpawns) do -- Loop goes through each child of the weaponSpawns folder
	
	-- Choses Weapons
	local weaponChoice = math.random(1, #weaponsChildren)
	print(weaponChoice)
	if weaponChoice == 1 then -- Pistol
		print("1 success")
		local weapon = weapons.Glock:Clone()
		weapon.Parent = child
		weapon.Handle.CFrame = child.CFrame
	elseif weaponChoice == 2 then -- AK47
		print("2 success")
		local weapon = weapons.AK:Clone()
		weapon.Parent = child
		weapon.Handle.CFrame = child.CFrame	
	elseif weaponChoice == 3 then -- Scar
		print("3 success")
		local weapon = weapons.Scar:Clone()
		weapon.Parent = child
		weapon.Handle.CFrame = child.CFrame
	elseif weaponChoice == 4 then -- AWP
		print("4 success")
		local weapon = weapons.AWP:Clone()
		weapon.Parent = child
		weapon.Handle.CFrame = child.CFrame
	end
end

print("there are "..weaponSpawnsAmount.." weapon spawns")
print("there are "..weaponCount.." current weapons")

The problem is that it only works for the glock. It wont parent any other tool to the WeaponSpawn, only the glock (if it chosen for that part). Heres some screenshots if your a lil confused, it should be working fine though?

It is printing stuff tho
image


(all of these WeaponSpawns should have Tools in them)

1 Like

local weapons = game:GetService("ServerStorage"):WaitForChild("Weapons")
local weaponSpawns = workspace:WaitForChild("GamePlay"):WaitForChild("WeaponSpawns"):GetChildren()
local weaponSpawnsAmount = #weaponSpawns

local weaponsChildren = weapons:GetChildren()
local weaponCount = #weaponsChildren

for i,v in pairs(weaponSpawns) do
   local randomWeapon = weaponsChildren[math.random(#weaponCount)]:Clone()
   randomWeapon.Parent = v
   randomWeapon.Handle.CFrame = v.CFrame
end

A little tip here is that when you’re using multiple conditional statements in a row, its probably a sign that there is a much simpler way of doing it that occupies less lines and is also much more efficient.

If this doesn’t work, can you send me a picture of the entirety of the Weapons folder in ServerStorage?

2 Likes

Still for some reason, it will only parent the Glock.


Its printing stuff tho
image

local weapons = game.ServerStorage.Weapons
local weaponSpawns = game.Workspace.GamePlay.WeaponSpawns:GetChildren()
local weaponSpawnsAmount = #weaponSpawns

local weaponsChildren = weapons:GetChildren()
local weaponCount = #weaponsChildren

for i,v in pairs(weaponSpawns) do
	local randomWeapon = weaponsChildren[math.random(weaponCount)]:Clone()
	print(randomWeapon)
	randomWeapon.Parent = v
	randomWeapon.Handle.CFrame = v.CFrame
	print("success")
end

image

That shouldn’t be happening at all. Everything is also in weaponsChildren right? Can’t really come up with any feasible explanation for this.

Heres my layout, image image

1 Like

If this is the case, I really have no idea why this is even happening in the first place.

Any possibility the weapons other than the Glock are just unanchored and falling out of the world?

I’ve tried this in a different world, the other weapons appear then immediacy disappear leaving only the glocks left, could the weapons be falling out of the world???

can you check if the weapon handles have the CanCollide property ticked off? That might be why.

In this line, you’re just referencing the weapon count, means the last weapon, whereas you should write

local randomWeapon = weaponsChildren[math.random(1, weaponCount)]:Clone()

The Glocks CanCollide = true the rest are false, this must be why!

1 Like

If I recall correctly, using just weaponCount works as is. You dont need to do 1, weaponCount.

It turns out, its worked this whole time, just the weapons were falling through the world (apart from the glock ofc)! Thanks for the help guys, ive turned CanCollide on for all the Weapons :sweat_smile::sweat_smile::sweat_smile::sweat_smile:

1 Like

LOL, no problem. I think this happens to every dev at some point.

1 Like

I was litterally contemplating giving up I was like theres no reason why this shouldnt be working!!! Just a simple mistake :sweat_smile:

There is another variation of this mistake which is even harder to spot: It’s when the items in ServerStorage already have CFrame positions below the kill height, and they instantly get destroyed when parented to workspace if you set the Parent before setting CFrame. It’s advised to set CFrame first, which also has a small performance benefit.

1 Like

Thanks for the information :+1::+1::+1: