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.
local checkName = "RubyRidgeWasAMassacre"
object.Touched:Connect(function(touched)
if touched.Name == checkName then
print("the government is not your ally")
end
end)
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
Ah, I understand now. Mine uses no models for my levels, only folders inside folders.
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.
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)
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?
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.
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.
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
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.
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.
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.