Tool not respawning in backpack!

Hello there I have this code for a weapon shop and I am wondering how I can have the equipped tool spawn in my backpack when i reset/leave.

local Player = script.Parent.Parent.Parent
local DataStoreService = game:GetService(“DataStoreService”)
local toolDataStore = DataStoreService:GetDataStore(“PlayerTools”)
local Time = nil
local Kills = nil

wait(3)
if Player:FindFirstChild(“leaderstats”) then
if Player.leaderstats:FindFirstChild(“Time”) then
Time = Player.leaderstats.Time
end
if Player.leaderstats:FindFirstChild(“Kills”) then
Kills = Player.leaderstats.Kills
end
end

local purchasedTools = {}
local equippedToolName = nil – Track equipped tool

local function loadPurchasedTools()
local success, loadedTools = pcall(function()
return toolDataStore:GetAsync(Player.UserId) or {}
end)

if success and loadedTools then
    purchasedTools = loadedTools
else
    warn("Failed to load purchased tools for " .. Player.Name)
end

end

local function savePurchasedTools()
local success, err = pcall(function()
toolDataStore:SetAsync(Player.UserId, purchasedTools)
end)
if not success then
warn("Failed to save purchased tools for " … Player.Name … ": " … err)
end
end

loadPurchasedTools()

local toolPrices = {}

for _, tool in pairs(game.ReplicatedStorage:WaitForChild(“Tools”):GetChildren()) do
if tool:IsA(“Tool”) then
local Price = tool:FindFirstChild(“Price”) and tool.Price.Value or 100
local Currency = tool:FindFirstChild(“Currency”) and tool.Currency.Value or “Time”

    table.insert(toolPrices, {tool = tool, price = Price, currency = Currency})
end

end

table.sort(toolPrices, function(a, b)
return a.price < b.price
end)

for _, toolInfo in ipairs(toolPrices) do
local tool = toolInfo.tool
local Price = toolInfo.price
local Currency = toolInfo.currency

local ToolFrame = script.ToolFrame:Clone()

ToolFrame.Size = UDim2.new(1, 100, 0, 50)
ToolFrame.BuyButton.Price.Text = Price .. "⏱"
ToolFrame.ToolName.Text = tool.Name
ToolFrame.ToolName.Name = tool.Name
ToolFrame.ImageLabel.Image = tool.TextureId
ToolFrame.Parent = script.Parent.ShopFrame.Frame

-- Initial button text setting based on tool ownership
local function updateButtonText()
    if table.find(purchasedTools, tool.Name) then
        local isEquipped = (equippedToolName == tool.Name)
        ToolFrame.BuyButton.Price.Text = isEquipped and "Unequip" or "Equip"
    else
        ToolFrame.BuyButton.Price.Text = Price .. "⏱"  -- Show price if not owned
    end
end

updateButtonText()  -- Set initial button text

-- Button Logic
ToolFrame.BuyButton.MouseButton1Click:Connect(function()
    local playerCurrency = (Currency == "Time") and Time or Kills

    if table.find(purchasedTools, tool.Name) then
        -- Equip or Unequip Logic
        if equippedToolName == tool.Name then
            -- Unequip the currently equipped tool
            equippedToolName = nil

            -- Remove the tool from the backpack
            local oldTool = Player.Backpack:FindFirstChild(tool.Name)
            if oldTool then
                oldTool:Destroy()  -- Remove the old equipped tool
            end
        else
            -- Equip the new tool
            local toolToEquip = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(tool.Name)
            if toolToEquip then
                -- Check if there's an already equipped tool to replace
                if equippedToolName then
                    local oldTool = Player.Backpack:FindFirstChild(equippedToolName)
                    if oldTool then
                        oldTool:Destroy()  -- Remove the old equipped tool
                    end
                end

                local newTool = toolToEquip:Clone()
                newTool.Parent = Player.Backpack
                equippedToolName = tool.Name  -- Update equipped tool name
            end
        end
    else
        -- Purchase Logic
        if playerCurrency.Value < Price then
            return  -- Don't do anything if not enough currency
        end

        playerCurrency.Value -= Price
        table.insert(purchasedTools, tool.Name)  -- Add the tool to purchased list

        -- Remove existing tool if necessary before adding the new one
        if equippedToolName then
            local oldTool = Player.Backpack:FindFirstChild(equippedToolName)
            if oldTool then
                oldTool:Destroy()  -- Remove the old equipped tool
            end
        end
        
        local newTool = tool:Clone()
        newTool.Parent = Player.Backpack  -- Add new tool to Backpack
        equippedToolName = tool.Name  -- Set new equipped tool name
    end

    updateButtonText()  -- Update button text after action
    savePurchasedTools()  -- Save purchased tools
end)

end

script.Parent.ShopButton.MouseButton1Click:Connect(function()
script.Parent.ShopFrame.Visible = not script.Parent.ShopFrame.Visible
end)

script.Parent.ShopFrame.ExitButton.MouseButton1Click:Connect(function()
script.Parent.ShopFrame.Visible = false
end)

game.Players.PlayerRemoving:Connect(function(p)
if p == Player then
savePurchasedTools()
end
end)

– Update equipped tool on Character addition
Player.CharacterAdded:Connect(function()
if equippedToolName then
local tool = game.ReplicatedStorage:WaitForChild(“Tools”):FindFirstChild(equippedToolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = Player.Backpack – Add to Backpack only
end
end
end)

Its all one script idk why its broken up

dear god

please use backticks ``` to show code
also PLEASE give the line where the error occurs

2 Likes

theres no errors im just wondering how should i go about doing it:

local DataStoreService = game:GetService("DataStoreService")
local toolDataStore = DataStoreService:GetDataStore("PlayerTools")
local Time = nil
local Kills = nil

wait(3)
if Player:FindFirstChild("leaderstats") then
	if Player.leaderstats:FindFirstChild("Time") then
		Time = Player.leaderstats.Time
	end
	if Player.leaderstats:FindFirstChild("Kills") then
		Kills = Player.leaderstats.Kills
	end
end

local purchasedTools = {}
local equippedToolName = nil  -- Track equipped tool

local function loadPurchasedTools()
	local success, loadedTools = pcall(function()
		return toolDataStore:GetAsync(Player.UserId) or {}
	end)

	if success and loadedTools then
		purchasedTools = loadedTools
	else
		warn("Failed to load purchased tools for " .. Player.Name)
	end
end

local function savePurchasedTools()
	local success, err = pcall(function()
		toolDataStore:SetAsync(Player.UserId, purchasedTools)
	end)
	if not success then
		warn("Failed to save purchased tools for " .. Player.Name .. ": " .. err)
	end
end

loadPurchasedTools()

local toolPrices = {}

for _, tool in pairs(game.ReplicatedStorage:WaitForChild("Tools"):GetChildren()) do
	if tool:IsA("Tool") then
		local Price = tool:FindFirstChild("Price") and tool.Price.Value or 100
		local Currency = tool:FindFirstChild("Currency") and tool.Currency.Value or "Time"

		table.insert(toolPrices, {tool = tool, price = Price, currency = Currency})
	end
end

table.sort(toolPrices, function(a, b)
	return a.price < b.price
end)

for _, toolInfo in ipairs(toolPrices) do
	local tool = toolInfo.tool
	local Price = toolInfo.price
	local Currency = toolInfo.currency

	local ToolFrame = script.ToolFrame:Clone()

	ToolFrame.Size = UDim2.new(1, 100, 0, 50)
	ToolFrame.BuyButton.Price.Text = Price .. "⏱"
	ToolFrame.ToolName.Text = tool.Name
	ToolFrame.ToolName.Name = tool.Name
	ToolFrame.ImageLabel.Image = tool.TextureId
	ToolFrame.Parent = script.Parent.ShopFrame.Frame

	-- Initial button text setting based on tool ownership
	local function updateButtonText()
		if table.find(purchasedTools, tool.Name) then
			local isEquipped = (equippedToolName == tool.Name)
			ToolFrame.BuyButton.Price.Text = isEquipped and "Unequip" or "Equip"
		else
			ToolFrame.BuyButton.Price.Text = Price .. "⏱"  -- Show price if not owned
		end
	end

	updateButtonText()  -- Set initial button text

	-- Button Logic
	ToolFrame.BuyButton.MouseButton1Click:Connect(function()
		local playerCurrency = (Currency == "Time") and Time or Kills

		if table.find(purchasedTools, tool.Name) then
			-- Equip or Unequip Logic
			if equippedToolName == tool.Name then
				-- Unequip the currently equipped tool
				equippedToolName = nil

				-- Remove the tool from the backpack
				local oldTool = Player.Backpack:FindFirstChild(tool.Name)
				if oldTool then
					oldTool:Destroy()  -- Remove the old equipped tool
				end
			else
				-- Equip the new tool
				local toolToEquip = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(tool.Name)
				if toolToEquip then
					-- Check if there's an already equipped tool to replace
					if equippedToolName then
						local oldTool = Player.Backpack:FindFirstChild(equippedToolName)
						if oldTool then
							oldTool:Destroy()  -- Remove the old equipped tool
						end
					end

					local newTool = toolToEquip:Clone()
					newTool.Parent = Player.Backpack
					equippedToolName = tool.Name  -- Update equipped tool name
				end
			end
		else
			-- Purchase Logic
			if playerCurrency.Value < Price then
				return  -- Don't do anything if not enough currency
			end

			playerCurrency.Value -= Price
			table.insert(purchasedTools, tool.Name)  -- Add the tool to purchased list

			-- Remove existing tool if necessary before adding the new one
			if equippedToolName then
				local oldTool = Player.Backpack:FindFirstChild(equippedToolName)
				if oldTool then
					oldTool:Destroy()  -- Remove the old equipped tool
				end
			end

			local newTool = tool:Clone()
			newTool.Parent = Player.Backpack  -- Add new tool to Backpack
			equippedToolName = tool.Name  -- Set new equipped tool name
		end

		updateButtonText()  -- Update button text after action
		savePurchasedTools()  -- Save purchased tools
	end)
end

script.Parent.ShopButton.MouseButton1Click:Connect(function()
	script.Parent.ShopFrame.Visible = not script.Parent.ShopFrame.Visible
end)

script.Parent.ShopFrame.ExitButton.MouseButton1Click:Connect(function()
	script.Parent.ShopFrame.Visible = false
end)

game.Players.PlayerRemoving:Connect(function(p)
	if p == Player then
		savePurchasedTools()
	end
end)

-- Update equipped tool on Character addition
Player.CharacterAdded:Connect(function()
	if equippedToolName then
		local tool = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(equippedToolName)
		if tool then
			local newTool = tool:Clone()
			newTool.Parent = Player.Backpack  -- Add to Backpack only
		end
	end
end)

it’s 3 backticks, it gives color

local DataStoreService = game:GetService("DataStoreService")
local toolDataStore = DataStoreService:GetDataStore("PlayerTools")
local Time = nil
local Kills = nil

wait(3)
if Player:FindFirstChild("leaderstats") then
	if Player.leaderstats:FindFirstChild("Time") then
		Time = Player.leaderstats.Time
	end
	if Player.leaderstats:FindFirstChild("Kills") then
		Kills = Player.leaderstats.Kills
	end
end

local purchasedTools = {}
local equippedToolName = nil  -- Track equipped tool

local function loadPurchasedTools()
	local success, loadedTools = pcall(function()
		return toolDataStore:GetAsync(Player.UserId) or {}
	end)

	if success and loadedTools then
		purchasedTools = loadedTools
	else
		warn("Failed to load purchased tools for " .. Player.Name)
	end
end

local function savePurchasedTools()
	local success, err = pcall(function()
		toolDataStore:SetAsync(Player.UserId, purchasedTools)
	end)
	if not success then
		warn("Failed to save purchased tools for " .. Player.Name .. ": " .. err)
	end
end

loadPurchasedTools()

local toolPrices = {}

for _, tool in pairs(game.ReplicatedStorage:WaitForChild("Tools"):GetChildren()) do
	if tool:IsA("Tool") then
		local Price = tool:FindFirstChild("Price") and tool.Price.Value or 100
		local Currency = tool:FindFirstChild("Currency") and tool.Currency.Value or "Time"

		table.insert(toolPrices, {tool = tool, price = Price, currency = Currency})
	end
end

table.sort(toolPrices, function(a, b)
	return a.price < b.price
end)

for _, toolInfo in ipairs(toolPrices) do
	local tool = toolInfo.tool
	local Price = toolInfo.price
	local Currency = toolInfo.currency

	local ToolFrame = script.ToolFrame:Clone()

	ToolFrame.Size = UDim2.new(1, 100, 0, 50)
	ToolFrame.BuyButton.Price.Text = Price .. "⏱"
	ToolFrame.ToolName.Text = tool.Name
	ToolFrame.ToolName.Name = tool.Name
	ToolFrame.ImageLabel.Image = tool.TextureId
	ToolFrame.Parent = script.Parent.ShopFrame.Frame

	-- Initial button text setting based on tool ownership
	local function updateButtonText()
		if table.find(purchasedTools, tool.Name) then
			local isEquipped = (equippedToolName == tool.Name)
			ToolFrame.BuyButton.Price.Text = isEquipped and "Unequip" or "Equip"
		else
			ToolFrame.BuyButton.Price.Text = Price .. "⏱"  -- Show price if not owned
		end
	end

	updateButtonText()  -- Set initial button text

	-- Button Logic
	ToolFrame.BuyButton.MouseButton1Click:Connect(function()
		local playerCurrency = (Currency == "Time") and Time or Kills

		if table.find(purchasedTools, tool.Name) then
			-- Equip or Unequip Logic
			if equippedToolName == tool.Name then
				-- Unequip the currently equipped tool
				equippedToolName = nil

				-- Remove the tool from the backpack
				local oldTool = Player.Backpack:FindFirstChild(tool.Name)
				if oldTool then
					oldTool:Destroy()  -- Remove the old equipped tool
				end
			else
				-- Equip the new tool
				local toolToEquip = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(tool.Name)
				if toolToEquip then
					-- Check if there's an already equipped tool to replace
					if equippedToolName then
						local oldTool = Player.Backpack:FindFirstChild(equippedToolName)
						if oldTool then
							oldTool:Destroy()  -- Remove the old equipped tool
						end
					end

					local newTool = toolToEquip:Clone()
					newTool.Parent = Player.Backpack
					equippedToolName = tool.Name  -- Update equipped tool name
				end
			end
		else
			-- Purchase Logic
			if playerCurrency.Value < Price then
				return  -- Don't do anything if not enough currency
			end

			playerCurrency.Value -= Price
			table.insert(purchasedTools, tool.Name)  -- Add the tool to purchased list

			-- Remove existing tool if necessary before adding the new one
			if equippedToolName then
				local oldTool = Player.Backpack:FindFirstChild(equippedToolName)
				if oldTool then
					oldTool:Destroy()  -- Remove the old equipped tool
				end
			end

			local newTool = tool:Clone()
			newTool.Parent = Player.Backpack  -- Add new tool to Backpack
			equippedToolName = tool.Name  -- Set new equipped tool name
		end

		updateButtonText()  -- Update button text after action
		savePurchasedTools()  -- Save purchased tools
	end)
end

script.Parent.ShopButton.MouseButton1Click:Connect(function()
	script.Parent.ShopFrame.Visible = not script.Parent.ShopFrame.Visible
end)

script.Parent.ShopFrame.ExitButton.MouseButton1Click:Connect(function()
	script.Parent.ShopFrame.Visible = false
end)

game.Players.PlayerRemoving:Connect(function(p)
	if p == Player then
		savePurchasedTools()
	end
end)

-- Update equipped tool on Character addition
Player.CharacterAdded:Connect(function()
	if equippedToolName then
		local tool = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(equippedToolName)
		if tool then
			local newTool = tool:Clone()
			newTool.Parent = Player.Backpack  -- Add to Backpack only
		end
	end
end)

Yeah i just edited the comment.

@jamesonmonroe

  1. store the names of the purchased tools, then get them upon player joining
  2. please tell gpt stop nesting your code, it makes it very unreadable

Alright I have them in a folder in replicated storage called Tools

got it to finally work thank you

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