Tool Not Being Destroyed

I am currently developing a game in which the “Tagger” will receive a new random weapon every 20 seconds. For some reason, any time the function WeaponClone:Destroy() is called, nothing happens. There is no error in the output. I’m not sure why this is happening.

My Code:

local Status = script.Countdown
local Players = game:GetService("Players")
local Image = script.Image
local WeaponText = script.WeaponText
local visible = game.ReplicatedStorage.Values.visible

while true do

	repeat
		wait(1)
	until game.ReplicatedStorage.Values.InGame.Value == true
	
	visible.Value = true
	
	local Tagger = Players[game.ReplicatedStorage.Values.Tagger.Value]
	print(Tagger)
	
	local Weapons = {}
	for i, v in pairs(game.ReplicatedStorage.Weapons:GetChildren()) do
		table.insert(Weapons, v.Name)
	end
	
	local RandomObj = Random.new()
	local Weapon = Weapons[RandomObj:NextInteger(1,#Weapons)]
	
	local WeaponClone = game.ReplicatedStorage.Weapons[Weapon]:Clone()
	WeaponClone.Parent = Tagger.Character
	
	Image.Value = WeaponClone.Image.Value
	WeaponText.Value = WeaponClone.Name
	
	repeat
		
		for i = 20,0,-1 do
			Status.Value = "("..i..")"
			if game.ReplicatedStorage.Values.GameEnd.Value == true then
				print("ended")
				break
			end
			wait(1)
		end
		
		WeaponClone:Destroy()
		
		local RandomObj = Random.new()
		local Weapon = Weapons[RandomObj:NextInteger(1,#Weapons)]

		local WeaponClone = game.ReplicatedStorage.Weapons[Weapon]:Clone()
		WeaponClone.Parent = Tagger.Character

		Image.Value = WeaponClone.Image.Value
		WeaponText.Value = WeaponClone.Name
		
	until game.ReplicatedStorage.Values.GameEnd.Value == true
	
	visible.Value = false
	WeaponClone:Destroy()
	Image.Value = 0
end

Could you try printing WeaponClone before you destroy it? Maybe that’ll clear some things up…

It looks like the issue is related to the scope of the WeaponClone variable. The WeaponClone defined inside the repeat block is local to that block, and when you try to destroy it outside the loop, it’s referencing a different variable with the same name.

To fix this, you should remove the local keyword when redefining WeaponClone inside the loop, so it doesn’t create a new local variable but updates the existing one:

local Status = script.Countdown
local Players = game:GetService("Players")
local Image = script.Image
local WeaponText = script.WeaponText
local visible = game.ReplicatedStorage.Values.visible

while true do
    repeat
        wait(1)
    until game.ReplicatedStorage.Values.InGame.Value == true

    visible.Value = true

    local Tagger = Players[game.ReplicatedStorage.Values.Tagger.Value]
    print(Tagger)

    local Weapons = {}
    for i, v in pairs(game.ReplicatedStorage.Weapons:GetChildren()) do
        table.insert(Weapons, v.Name)
    end

    local RandomObj = Random.new()
    local Weapon = Weapons[RandomObj:NextInteger(1, #Weapons)]

    local WeaponClone = game.ReplicatedStorage.Weapons[Weapon]:Clone()
    WeaponClone.Parent = Tagger.Character

    Image.Value = WeaponClone.Image.Value
    WeaponText.Value = WeaponClone.Name

    repeat
        for i = 20, 0, -1 do
            Status.Value = "(" .. i .. ")"
            if game.ReplicatedStorage.Values.GameEnd.Value == true then
                print("ended")
                break
            end
            wait(1)
        end

        WeaponClone:Destroy()

        local RandomObj = Random.new()
        Weapon = Weapons[RandomObj:NextInteger(1, #Weapons)]  -- Remove "local" here

        WeaponClone = game.ReplicatedStorage.Weapons[Weapon]:Clone()  -- Remove "local" here
        WeaponClone.Parent = Tagger.Character

        Image.Value = WeaponClone.Image.Value
        WeaponText.Value = WeaponClone.Name

    until game.ReplicatedStorage.Values.GameEnd.Value == true

    visible.Value = false
    WeaponClone:Destroy()
    Image.Value = 0
end
1 Like

Thank you, this helped so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.