How can I delete a part that I made with Instance.new?

I am trying to make a bat as a test to help me learn scripting, but I’ve come along an issue where equipping the bat floods the torso with Motor6Ds, which I will use to animate the swing.
image
The plan was to delete them after the bat is unequipped, and when the bat is equipped the Motor6D will be there to animate the swing.

The issue is I’m not sure how I can delete the Motor6D after I am done with it?
Thanks for helping in advance, but here is a picture of my very noob code as a sample of what I used.

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tool = script.Parent
local player = game.Players.LocalPlayer

Tool.Equipped:connect(function()
local Motor = Instance.new ("Motor6D")
Motor.Parent = script.Parent.Parent.Torso
end)
Tool.Unequipped:Connect(function()
Motor = nil

end)

Give it a name! Like this:

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tool = script.Parent
local player = game.Players.LocalPlayer

Tool.Equipped:connect(function()
local Motor = Instance.new ("Motor6D")
Motor.Name = "NewMotor"
Motor.Parent = script.Parent.Parent.Torso
end)
Tool.Unequipped:Connect(function()
Motor = nil

-- do what ever you want here'

NewMotor:Destroy()

end)

Hope this helped, have a good day!

It should be just Motor:Destroy(). Adding a name won’t really do anything.

Well it was a suggestion, and it works.

Then add a name and get it.

local motor = workspace:FindFirstChild("NewMotor1")
motor:Destroy()
motor = workspace:FindFirstChild("NewMotor2")
motor:Destroy

You need to call :Destroy on an instance not on the instance name.
In your script there’s no NewMotor variable so it would error.

When I dequip the tool, the script disappears with it so I can’t find the Torso through script.parent.
Is there a way to find the Motor6D even when the script is not inside the player’s model itself?

And I can’t do workspace.CrazyGrapefruit32 because that will not work for other players, so I’m lost.

You can get the player character from a local script using game.Players.LocalPlayer.Character.

You can try use this and see if it works, however you might need to change the code up a bit as to when you want it deleted @CrazyGrapefruit32, dm me if you have any questions.

But it wont work for other players

It will. It gets the local player.

Is this is a local or a server script?


image
image

You can’t get LocalPlayer using server script. To get the player from normal Script you would need to use a RemoteEvent or a loop.

You mean a server script, not a local script.

You can get a local player using a local script

If i move the code to a local script, will the animation only show client side?

There is no animation in the script

if i were to add one that is, because that was the plan

Then yes it will be as its a local script, it all happens locally

So in that case, id have to use a server script. So how would a fella get the character then?

define the character once the tool is equipped