So I have a box system and when the player touches the box I want the box to destroy. It works fine if there is one box in workspace but if there is two the boxes do not delete.
m = require(game:GetService("ServerScriptService").MainModule)
spawnArea = game.Workspace:WaitForChild("BoxSpawnArea")
function newNormalBox(cash)
box = game:GetService("ReplicatedStorage"):WaitForChild("Boxes"):FindFirstChild("NormalBox"):Clone()
box.Parent = game.Workspace
box.Position = Vector3.new(math.random(-spawnArea.Size.x/2,spawnArea.Size.x/2),125,math.random(-spawnArea.Size.z/2,spawnArea.Size.z/2))
local AlreadyHit = false
local function touch(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum~=nil and AlreadyHit ~= true then
game.Workspace.SoundsEffect.BoxHit:Play()
AlreadyHit = true
game.Players[m.PlayerName].leaderstats.Cash.Value = game.Players[m.PlayerName].leaderstats.Cash.Value + cash
box:Destroy()
end
end
box.Touched:Connect(touch)
end
function newGemBox(gems)
box = game:GetService("ReplicatedStorage"):WaitForChild("Boxes"):FindFirstChild("GemBox"):Clone()
box.Parent = game.Workspace
box.Position = Vector3.new(math.random(-spawnArea.Size.x/2,spawnArea.Size.x/2),125,math.random(-spawnArea.Size.z/2,spawnArea.Size.z/2))
local AlreadyHit = false
local function touch(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum~=nil and AlreadyHit ~= true then
game.Workspace.SoundsEffect.BoxHit:Play()
AlreadyHit = true
m.Gems = m.Gems + gems
box:Destroy()
end
end
box.Touched:Connect(touch)
end
function newGoldBox(cash,gems)
box = game:GetService("ReplicatedStorage"):WaitForChild("Boxes"):FindFirstChild("GoldBox"):Clone()
box.Parent = game.Workspace
box.Position = Vector3.new(math.random(-spawnArea.Size.x/2,spawnArea.Size.x/2),125,math.random(-spawnArea.Size.z/2,spawnArea.Size.z/2))
local AlreadyHit = false
local function touch(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum~=nil and AlreadyHit ~= true then
game.Workspace.SoundsEffect.BoxHit:Play()
AlreadyHit = true
game.Players[m.PlayerName].leaderstats.Cash.Value = game.Players[m.PlayerName].leaderstats.Cash.Value + cash
m.Gems = m.Gems + gems
box:Destroy()
end
end
box.Touched:Connect(touch)
end
function newDiamondBox(cash,gems)
box = game:GetService("ReplicatedStorage"):WaitForChild("Boxes"):FindFirstChild("DiamondBox"):Clone()
box.Parent = game.Workspace
box.Position = Vector3.new(math.random(-spawnArea.Size.x/2,spawnArea.Size.x/2),125,math.random(-spawnArea.Size.z/2,spawnArea.Size.z/2))
local AlreadyHit = false
local function touch(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum~=nil and AlreadyHit ~= true then
game.Workspace.SoundsEffect.BoxHit:Play()
AlreadyHit = true
game.Players[m.PlayerName].leaderstats.Cash.Value = game.Players[m.PlayerName].leaderstats.Cash.Value + cash
m.Gems = m.Gems + gems
box:Destroy()
end
end
box.Touched:Connect(touch)
end
chance = {1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,4}
while wait(.10) do
a = chance[math.random(1,#chance)]
warn(a)
if a == 1 then
newNormalBox(math.random(50,500))
else if a == 2 then
newGemBox(math.random(1,3))
else if a == 3 then
newGoldBox(math.random(2000,4000),math.random(1,10))
else if a == 4 then
newDiamondBox(math.random(20000,50000),math.random(5,40))
end
end
end
end
game.StarterGui:SetCore("SendNotification",{
Title = "NOTICE";
Text = "A box has spawned somewhere in the map. You should go see what's inside!";
Icon = "";
Duration = 5;
})
end

2 comments
You have to do a for loop or name the boxes different names and then Remove() them
I gave the boxes each a unique id in there name but I still get the same problem @seith14