Adding and Removing Tools when levelling up

Having trouble writing a piece of code that gives tools based on a level and removes the previous level tools. So if I’m Level 2 I should gain a set of tools for level 2 and the level 1 tools should be removed. With this code the tools for level 2 get added but the level 1 tools remain. I also feel like my code is really inefficient.This script is activated when ofc I level up and a remote event is fired. Any ideas or support would be much appreciated.

local RS = game:GetService('ReplicatedStorage')
local giveTool = RS.GiveTool 
local L1Tools = game.ServerStorage:WaitForChild('Tools').Level1
local L2Tools = game.ServerStorage:WaitForChild('Tools').Level2

giveTool.OnServerEvent:Connect(function(player,level)
	local character = player.Character
	local backpack = player:WaitForChild('Backpack')
	
	local L1ToolsD = {L1Tools.L1}
	local L2ToolsD = {L2Tools.L2}
	
	if level.Value == 1 then 
		
		for i,v in pairs(L1ToolsD) do
			if player and character then 
				v:Clone().Parent = backpack 
			end 
		end
		
	elseif level.Value == 2 then 
		local function RemoveTools2(container)
			for i,v in pairs(L1ToolsD) do 
				local tools = container:FindFirstChild(v)
				if tools and tools:IsA('Tool') then
					tools:Destroy()
					end
				end 
		end
		RemoveTools2(character)
		RemoveTools2(backpack)
		
		for i,d in pairs(L2ToolsD) do 
				if player and character then 
					d:Clone().Parent = backpack 
				end 
			end
		
	end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

writing your code like this

if stat == 1 then

elseif stat == 2 then

-- etc

Is extremely messy and will get really frustrating when you get up to level 10 or so. This is a much better way that makes it that you don’t have to make the code longer when you add more levels.

local RS = game:GetService('ReplicatedStorage')
local giveTool = RS.GiveTool 
local Tools = game.ServerStorage:WaitForChild('Tools')

giveTool.OnServerEvent:Connect(function(player, level)
	local character = player.Character
	local backpack = player:WaitForChild('Backpack')

    local NewTools = Tools["Level" .. level]:GetChildren() -- .. attaches "Level" and whatever value level is into one string. GetChildren gets a table with all the children
    local OldToolsCheck = Tools:FindFirstChild("Level" .. (level - 1))
    local OldTools

    if OldToolsCheck then
        OldTools = OldToolsCheck:GetChildren()
    end

    if OldTools then
        local function RemoveTools(container)
			for i,v in pairs(OldTools) do 
				local tool = container:FindFirstChild(v)
				if tool and tool:IsA('Tool') then
					tool:Destroy()
				end
			end 
		end

		RemoveTools(character)
		RemoveTools(backpack)
    end

	for i,d in pairs(NewTools) do 
		if player and character then 
			d:Clone().Parent = backpack 
		end 
	end
end)

Thank you so much! This actually helped a lot in making it more efficient. Only problem was that it still wasn’t deleting the tool when levelling up but realised that the tool was returning nil because you needed v.Name instead of v.

1 Like

Oh true, I didn’t notice that