Tools replacing each other when put in the player's backpack

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear! I would like to have a functioning tool shop.

  2. What is the issue? Include screenshots / videos if possible! When I buy one tool, it works alright, but when I buy another one, it goes into the players backpack, but destroys the first tool. When I equip an tool, I’m able to buy another one without replacing it, but as soon as I unequip it, it is destroyed. I have never come across this issue in my years of scripting.


As you can see in this video, buying a tool replaces the already existing one.

local Player = script.Parent.Parent.Parent
local leaderboard = nil
local Gems = nil

task.wait(3)
if Player:FindFirstChild("leaderboard") then
	if Player.leaderboard:FindFirstChild("Gems") then
		Gems = Player.leaderboard.Gems
	end
end
if Player:FindFirstChild("leaderstats") then
	if Player.leaderstats:FindFirstChild("Gems") then
		Gems = Player.leaderstats.Gems
	end
end
for _, tool in pairs(game.ReplicatedStorage:WaitForChild("Tools"):GetChildren()) do
	if tool:IsA("Tool") then
		local ToolFrame = script.ToolFrame:Clone()
		if tool:FindFirstChild("Price") then
			Price = tool:FindFirstChild("Price").Value
			ToolFrame.Price.Text = tool:FindFirstChild("Price").Value.." GEMS"
		end
		local Price = 100
		ToolFrame.Price.Text = "100 GEMS"
		ToolFrame["Item Name"].Text = tool.Name
		ToolFrame["Item Name"].Name = tool.Name
		ToolFrame.Display.ImageLabel.Image = tool.TextureId
		ToolFrame.Parent = script.Parent.Frame["Rectangle 30"]
		
		-- this is the part that puts the tool in the player's backpack upon purchase
		
		ToolFrame.Buy.PURCHASE.MouseButton1Click:Connect(function()
			if Player.Backpack:FindFirstChild(tool.Name) then
				warn("already purchased!")
			else
				if Gems == nil or Gems.Value < Price then
				script.Parent.LocalScript.RemoteEvent:FireAllClients(false)
				ToolFrame.BuyButton.BackgroundColor3 = Color3.fromRGB(255, 28, 43)
				task.wait(1)
				ToolFrame.BuyButton.BackgroundColor3 = Color3.fromRGB(41, 255, 66)
				return
			end
			Gems.Value -= Price
			tool:Clone()
			tool.Parent = Player.Backpack
			script.Parent.clocal.RemoteEvent:FireAllClients(true)
			end
		end)
	end
end
  1. What solutions have you tried so far? I have searched far and wide across the internet, but cannot find an issue quite like mine. It must be something very simple that I have overlooked, but I can’t figure it out. Any help is appreciated. Thank you!

Shouldn’t you store the instance you cloned in a new variable?

You might be reparenting the original tool instance and not the newly created tool clone.

Edit: I would try this:

		local clonedTool = 	tool:Clone()
			clonedTool .Parent = Player.Backpack

they’re cloning the tool into the same parent as the original tool, then the original tool is the one that goes into the backpack, the script will then set the cloned tool as the original one

I set the cloned instance to a variable, and printed them to the output. They each printed as separate tools such as “sword” and “scythe”, however, they still replaced each other when put in the player’s backpack.

That is odd, do you have any other scripts related to changing the tools parent or the tool being destroyed?

Perhaps it’s a server to client synchronization issue. I am assuming the current script you sent is a server script so it shouldn’t be an issue in that case.

I am unaware of any other scripts changing the cloned tools. Yes, this is a server script.

I solved it, not really sure how this happens but there was a data store script I found that was affecting the UI in some way. I disabled it and now I’m gonna rewrite it. Thanks to everyone that helped!

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