How do i make a script find a letter in a parts name

So i want to know how could i destroy a part once it touches another part with a keyword in it

this is my current script:

script.Parent.Touched:Connect(function(touched)
	if string.find(touched.Name, "d_", 2) then --cant figure out how would the script detect a keyword in a string
		touched:Destroy()
		print("destructed")
	else
		print("not destructable")
	end
end)

At first to make it work you don’t need that third argument so it would be string.find(touched.Name, "d_") also if you use touch event only in destructible parts you can disable “Can touch” property for non destructible parts and then your code will be:

script.Parent.Touched:Connect(function(touched)
        touched:Destroy()
end)

if some one touch some part anoter part destroy?

its good but you dont type who want to touch to destroy

i want it to only destroy parts that that have “d_” at the beginning of they’re name

just remove the third argument in string.find and should do

the parts names are random. Example: d_trash, d_body, d_tool

could do

if string.match(string.sub ( touched.Name, 1, 2), 'd_')  then    -- this would check just the first 2 letters of the name
2 Likes
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		script.Parent:Destroy()
	end
end)

ddddddd
you can make Folder for parts

and another option you could use CollectionService to tag the parts that can be destroyed this can be done at runtime also or in scripts

You are completely going off track on what i needed. Its already been solved by @Nyonic

when i type i dont see nyonic topic

nyonic topic best then my topic like i see(;

This also works without string.match

if string.sub(touched.Name, 1, 2) == "d_" then

1 Like
if string.sub(touched.Name, 1, 2) == "d_" then
	--Do code.
end

if string.find(touched.Name, "^d_") then
	--Do code.
end

if string.match(touched.Name, "^d_") then
	--Do code.
end