The code below is used to spawn in a random new tool. It works once, by spawning in the weapons that appear at the start of the game, then the one from the if statement, but then doesn’t work again. I tried putting it in a loop with while true do, but it doesn’t help. No errors in the output, either. Any solutions? I’m thinking it’s something simple I’m forgetting, lol. Thanks!
EDIT: the loop breaks it even more, in fact, by spawning a lot of weapons at once.
local chosen = nil
local weps = game:GetService("ReplicatedStorage").WeaponSpawns:GetChildren()
chosen = weps[math.random(1, #weps)]:Clone()
chosen.Parent = script.Parent
chosen:SetPrimaryPartCFrame(script.Parent.Location.CFrame)
while true do
wait(1)
if chosen ~= nil then
chosen.Hitbox.Touched:Connect(function()
wait(2)
chosen = weps[math.random(1, #weps)]:Clone()
chosen.Parent = script.Parent
chosen:SetPrimaryPartCFrame(script.Parent.Location.CFrame)
end)
end
end
local chosen = nil
local debounce = false
local weps = game:GetService("ReplicatedStorage").WeaponSpawns:GetChildren()
while true do
if debounce == false then
debounce = true
chosen = weps[math.random(1, #weps)]:Clone()
chosen.Parent = script.Parent
chosen:SetPrimaryPartCFrame(script.Parent.Location.CFrame)
wait(1) -- do the wait time you want
debounce = false
if chosen ~= nil then
chosen.Hitbox.Touched:Connect(function()
wait(2)
chosen = weps[math.random(1, #weps)]:Clone()
chosen.Parent = script.Parent
chosen:SetPrimaryPartCFrame(script.Parent.Location.CFrame)
end)
end
end
end
The problem with this, is that I don’t want multiple tools spawning over each other, which is the purpose of the if statement that checks to see if the weapon has been touched and therefore picked up.