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

if Player:FindFirstChild("leaderboard") then
	if Player.leaderboard:FindFirstChild("Gems") then
		Gems = Player.leaderboard.Gems
if Player:FindFirstChild("leaderstats") then
	if Player.leaderstats:FindFirstChild("Gems") then
		Gems = Player.leaderstats.Gems
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"
		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
			if Player.Backpack:FindFirstChild(tool.Name) then
				warn("already purchased!")
				if Gems == nil or Gems.Value < Price then
				ToolFrame.BuyButton.BackgroundColor3 = Color3.fromRGB(255, 28, 43)
				ToolFrame.BuyButton.BackgroundColor3 = Color3.fromRGB(41, 255, 66)
			Gems.Value -= Price
			tool.Parent = Player.Backpack
  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.