Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to change this script to work with all parts with a specific name?

Hi there, I'm wanting to make this script respawn anyone who touches a part with a specific name instead of having to place the script inside every part. Thanks :)

script.Parent.Touched:connect(function(Hit)
	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if Player then
		Player:LoadCharacter()	
	end
end)

3 comments

0

if Player.Name == "The name you want" then Player:LoadCharacter().

0

But, how would the player touch the part if he doesn't have a body? (Lenny)

1

its supposed to be that if the player walks on any brick with a specific name, for example "lava" then they get respawned

1 answer

Hi. What we want to look at is for loops, and the :GetChildren() and/or :GetDescendants() functions. How these work is they return an array with a list of all objects in a folder or model, so that they can be interacted with by a script.

First thing you're going to want to do is find some way of grouping all the parts. If you can put them in one folder in the workspace, that would be ideal, but if you can't, we can find them by name (which is what I assume you're doing right now.)

for i,child in pairs(game.Workspace:GetDescendants()) do --Goes through every single descendant of the workspace. If you can narrow this down, this will be less memory-intensive.
	if child.Name == "TouchPart" then --If the part has the name you're looking for, run the event for it. 
		child.Touched:Connect(function(Hit)
   			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    			if Player then
        			Player:LoadCharacter()  
    			end
		end)
	end
end

If you have any more questions about this or how any of this code works, let me know.

4 comments

0

Is there a way to make it so that it detects the name of parts the player is walking on instead of detecting through the part itself? Thanks :)

0

^ you could loop through the descendants of the character, check if they're parts, and connect to their Touched events. then, you could check what the name of whatever touches it is

0

it'll work either way though

0

Yeah you can connect it to the character, but then it's firing a lot more events.

This discussion is preserved as it was. New answers, comments, and votes are closed.