How to destroy a part when a player has stepped on it, but with no scripts in the part itself?

I know this seems like a stupid post because this is simple, but I can’t find a solution to this online.

I will have more than 2-3k parts. These parts will be cloned, but I just don’t want every one of them to have a script. I’m not asking you to give me a working script, but I would appreciate a start!

Thanks!

5 Likes

Connect .Touched on the part you cloned.

2 Likes

Well, I havent ever needed this, but I think you can try naming each of these parts something certain, then check if the player’s legs (r6) or feet (r15) touch the named parts.

try this

local partName = "your part name here"

script.Parent.RightFoot.Touched:Connect(function(t)
if t.Name == partName then
--Your code here
end
end)

Put this in StarterCharacterScripts

3 Likes

You could use CollectionService for something like this, give the part a tag then clone it as many times as you would like. I recommend you look it up on Youtube, or on the forums. It’s pretty straightforward and quite easy to use.

2 Likes

You forgot to close the function. :grin:

2 Likes

Why don’t you use loops??? Just put them in a folder and loop to see if they touch.

1 Like
for _, Parts in pairs(game.Workspace.Folder:GetChildren()) do
	Parts.Touched:Connect(function()
		Parts:Destroy()
	end)
end
1 Like

This works (on a normal character), but on my character, which has only 1 part (humanoid root part) it doesn’t. Do you know how I could fix this? I put this as a script in StarterCharacterScripts

1 Like

If you clone all the parts and parent them to a model or folder it becomes ridiculously easy. You can loop through each part on your character and add a touched event. After this you can just check if the part it touched is the child of the cloned model using like hit.Parent == workspace.Clone or whatever and if it is then just hit:Destroy(). Hope this helps I guess.

Edit: This is better than adding a connection to every part because you’re adding a connection to 2-3k parts whereas with this one you’re only adding it to like 10 parts.

1 Like

Assuming your parts are in a folder in the workspace called like Clone, your code would look something like this:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
    for _, part in pairs(char:GetChildten()) do
            if not part:IsA(“BasePart”) then continue end
            part.Touched:Connect(function(hit)
                if hit.Parent == workspace.Clone then hit:Destroy() end
            end)
        end
    end)
end)

Edit:
Wrote this on mobile so it will probably error but it’s just a concept.

Use magnitude check if the distance between the RootPart is 0.1 then count it?

You would be iterating through 2-3k parts to check if it is close by and it wouldn’t be accurate because it would be generating a sphere instead of a box, so this wouldn’t be a good option.