I've been trying to have something like this for a while for a project I've been working on. However, I don't seem to have any luck. What I need is a part that spawns a random weapon out of an assigned weapon pool. I currently have one that does exactly that, but weapons made out of more parts only spawn the handle, and they're definitely unusable in that state.
wait(1)
local weps = game.Lighting.Weapons:children() local wep = script.Parent:findFirstChild("Weapon") local chosen = nil while true do chosen = weps[math.random(1, #weps)] chosen.Parent = wep chosen.Handle.Position = script.Parent.Spawn.Position repeat wait() until chosen.Parent ~= wep or chosen:findFirstChild("Handle") == nil if chosen:findFirstChild("Handle") == nil then chosen:remove() end print("Tool was taken") print("Fetching new weapon...") wait(10) if wep == nil then local model = Instance.new("Model") model.Parent = script.Parent model.Name = "Weapon" local wep = model end end
This is the code I use. The script finds the weapons from a model.
You should use the Clone function to copy the weapon and place the copy into Workspace. You also shouldn't use the remove function as it has been deprecated, you should use Destroy instead. You need to set the position of the whole model and not just the handle and you can move the whole model by using the MoveTo function. This should work depending on how your hierarchy is set up.
local weps = game.Lighting.Weapons:children() local wep = script.Parent:findFirstChild("Weapon") local chosen = nil while true do chosen = weps[math.random(1, #weps)]:Clone() chosen.Parent = wep chosen:MoveTo(script.Parent.Spawn.Position) repeat wait() until chosen.Parent ~= wep or chosen:findFirstChild("Handle") == nil if chosen:findFirstChild("Handle") == nil then chosen:Destroy() end print("Tool was taken") print("Fetching new weapon...") wait(10) if wep == nil then local model = Instance.new("Model") model.Parent = script.Parent model.Name = "Weapon" local wep = model end end