Ad
Log in to vote
0

How can I make a random weapon spawner that spawns multi-part weapons?

Asked by 7 years ago

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.

0
Use the Code Block button instead of the Inline Code button. Unclear 1776 — 7y
0
That's because you are only changing the position of the handle on line 8. FearMeIAmLag 1161 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

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

0
It doesn't work... However, one thing I noticed is that the old script took weapons from lightning too, but they were actually in a workspace group. This script doesn't spawn the items at all, not even the handles. Should I upload the system I use as a model? sergiu8957 5 — 7y
Ad

Answer this question