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)
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.