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?
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?
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
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???
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
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.