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.