How would I destroy a tool and then clone a different tool in the same script?

Okay so, I’m making a simulator and I scripted this rebirth system. There is no errors, I’m just trying to make it so when I rebirth, the current tool you have in your backpack gets destroyed and a different tool gets cloned into your inventory. This is also in a normal script and it is not inside of a tool. It is inside of the ServerScriptService.

image

Try this:

game.ReplicatedStorage.Rebirths.OnServerEvent:Connect(function(player)
	if player.leaderstats.Rebirths.Value >= 1 then
		local backpack = player:WaitForChild('Backpack')
		local OldMic = backpack:FindFirstChildOfClass('Tool')
		local NewMic = game.ReplicatedStorage:WaitForChild('Microphone')
		if OldMic then
			OldMic:Destroy()
		end
		NewMic:Clone().Parent = backpack
	end
end)

Note: Keep in mind that if a player dies, their backpack is cleared once they respawn.

Hello!

The problem in your script is that you’re not setting NewMic’s parent to something after you have cloned it. You’ll want to update your last lines to the following:

local NewMic = game.ReplicatedStorage:WaitForChild("Microphone")
OldMic:Destroy()
NewMic.Parent = player.Backpack

That should fix your issue. Also, while we’re here, may I recommend that you can indent your code and it will look even better! It will also make it easier to read, and I can promise you it will be better. You should also call define ReplicatedStorage as its own variable :slight_smile:

Let me show a quick example:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.Rebirths.OnServerEvent:Connect(function (player)
    if player.leaderstats.Rebirths.Value >= 1 then
        local OldMic = player.Backpack:FindFirstChildOfClass("Tool").player.Backpack
        local NewMic = ReplicatedStorage:WaitForChild("Microphone")

        OldMic:Destroy()
        NewMic.Parent = player.Backpack
    end
end)

Oh now I know Thanks for the solution! This helps a lot and gives me a lot more experience now!

1 Like

Actually never mind, It doesn’t work. When I try to rebirth, the tool I currently have does not get destroyed and the other microphone doesn’t get cloned into my inventory.

Could you please provide the script that calls the remote, and are there any errors?

Sure. And no there are no errors.

Here is the script that calls the remote.