How do I check if a player touches a part with a specific name?

Hey programmers, I’m still quite new to programming and I was wondering if there was a way if a humanoid touched a part with a specific name. I don’t want to copy paste a script into each of the parts and was wondering if this was possible.

Thanks in advance!

3 Likes
local checkName = "RubyRidgeWasAMassacre"
object.Touched:Connect(function(touched)
     if touched.Name == checkName then
          print("the government is not your ally")
     end
end)
6 Likes

Tried to configure with this code and now I’m getting this error, it’s only when I touch the part though, and the “object”

This is line 15:

object.Touched:Connect(function(touched)

This is Object

local object = workspace:WaitForChild("Levels"):GetDescendants()

Is there anything I am doing wrong?

image

2 Likes

GetDescendants() returns a table, therefore has no EventListener property (idk what its called) so .Touched is defined as nil since it doesn’t exist.
Also, unless you want to get all the children of children (ie. if a part was inside of a part inside of Levels then both of them would be in the table) you should use :GetChildren() instead. Also, models do not have this property either. LMK if you use models inside of this script.

local checkName = "Roblox"
local folder = workspace:WaitForChild("Levels"):GetChildren()
for _, level in Levels do
     object.Touched:Connect(function(touched)
          if touched.Name == checkName then
              print("roblox")
          end
     end)
end
4 Likes

Ah, I understand now. Mine uses no models for my levels, only folders inside folders.
image

This is the code I am currently working with, not the best but it’s the best I know with

wait(1)
local checkName = "Complete"

local levels = workspace:WaitForChild("Levels"):GetChildren()

local repStorage = game:GetService("ReplicatedStorage")
local remotes = repStorage:WaitForChild("Remotes")
local SendMessage = remotes:WaitForChild("SendMessage")

local debounce = true

local touchPart = script.Parent
local plr = game:GetService("Players")
for _, level in levels do
	object.Touched:Connect(function(touched)
		if touched.Name == checkName then
			if debounce == true  and touched.Parent:FindFirstChild("Humanoid") then
				debounce = false
				local name = touched.Parent.Name
				game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",
				{
					Text = "[SERVER]: "..name.." has completed "..touchPart.Parent.Name.."!",
					Color = Color3.fromRGB(137, 255, 129),
					Font = Enum.Font.ArialBold,
					TextSize = 14,
				}
				)
				task.wait(2)
				touched.Parent:FindFirstChild("Head"):Destroy()
				task.wait(3)
				debounce = true
			end
		end
	end)
end

I thought adding a wait(1) would work before, but object still had a “nil” connection. I renamed ‘local object’ to ‘local levels’ and thought that would work, but now I have no idea what should be what.

I am still getting the exact same error though.

2 Likes

Just realised I was using the wrong way to send a systemMessage, oops.

But yeah do you mind helping what “object” should be? I thought it would be CheckName but I am pretty positive that would not work.

2 Likes

Use module scripts,thats why module scripts are necessary,put your saved varaibles in there and just grab it from there.then u can use it like mutiple times(if u need it)

2 Likes

What you are saying makes no sense, there is no reason for a module script.

2 Likes

My mistake, I gave you some slightly incorrect code.

local checkName = "Complete"

local levels = workspace:WaitForChild("Levels"):GetChildren()

local repStorage = game:GetService("ReplicatedStorage")
local remotes = repStorage:WaitForChild("Remotes")
local SendMessage = remotes:WaitForChild("SendMessage")

local debounce = true

local touchPart = script.Parent
local plr = game:GetService("Players")
for _, level in levels do
        if level:IsA("Folder") then continue end
	level.Touched:Connect(function(touched)
		if touched.Name == checkName then

		end
	end)
end

This is what you would do if everything in the folder is a part. Can you show the full contents of the folder? Do you want this to happen if any one of any of those parts that are in those folders is touched?

3 Likes

My bad, the game does use models but only for assets and certain gameplay. But not these exit doors.

It looks something like this, and “Complete” is just a Part at the very end of the level.
image

This is what the game pretty much is, only possible with 2 players though so I skipped through it entirely.


Sorry for the bad quality, had to compress it down to be under 10MB

When the player touches the white neon block called “Complete”, it should display a system message in the chat that someone completed the challenge and “reset” their character, going back to spawn.

I have tested the new script. no error is printed but no systemMessage gets published. I will try to debug to see where the issue lies.

3 Likes

Ok so I was debugging, found out because it’s only getting the children inside the Levels Folder, it’s not seeing what else is inside that. The complete parts are inside those folders and cannot figure it out.

I had a spare Complete door that was outside those folders, and only in the Levels folder for debugging purposes. It detects that it is touched but does not print that it checks that it is the correct name, even though the name is correct.

Here is my debugging attempt. Added comments to where it prints and doesn’t,

for _, level in levels do
	if level:IsA("Folder") then warn("Level is a Folder Property") --[[prints 4 times as there are 4 folders inside "Levels" folder.]] continue end
	level.Touched:Connect(function(touched)
		warn("Touched") -- prints
		if touched.Name == checkName then
			warn("Touched is correct Name") -- does not print
			if debounce == true and touched.Parent:FindFirstChild("Humanoid") then
				warn("Found Humanoid, debouncing") -- does not print
				debounce = false
				local name = touched.Parent.Name
				game:GetService("TextChatService").TextChannels.RBXSystem:DisplaySystemMessage(
					"<font color=\"rgb(137, 255, 129)\">[SERVER]: "..name.." has completed "..touchPart.Parent.Name.."!</font> "
				)
				task.wait(2)
				touched.Parent:FindFirstChild("Head"):Destroy()
				task.wait(3)
				debounce = true
				warn("Finish") -- does not print
			end
		end
	end)
end
2 Likes

I tell u a story,there is one post sayong about remote events is not working,i need to have 60++ posts to get the solution,

2 Likes

Remotes were there from an old code, forgot to remove them previously. Silly me.

2 Likes

Solution: it will not work because u should do this

Print(touched.Name)see what u will get

2 Likes

Didn’t know why I didn’t think of this, thank you. Seems like it’s getting the touched value of whatever part of the player is touching it. Not what the player IS touching, thanks for that.

2 Likes

So far, touched = part of the player that touched it, not what the player is touching. Didn’t know why that didn’t come across me. How would I do that exact thing but basically do the opposite, to where it’s what the player is touching gets the name of it, not what the player is touching the part with.

2 Likes

also print level.Name/ use it in the if statement

1 Like

image
Getting these

2 Likes

A friend also told me that CollectionService would be better to use but I have no knowledge of that. I’m reading through the document and still don’t know how to use it properly.

2 Likes

The thing is you need a tag and use that tag to run