Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)
0
if Player.Name == "The name you want" then Player:LoadCharacter(). mixgingengerina10 223 — 5y
0
But, how would the player touch the part if he doesn't have a body? (Lenny) mixgingengerina10 223 — 5y
1
its supposed to be that if the player walks on any brick with a specific name, for example "lava" then they get respawned kieranhendy 28 — 5y

1 answer

Log in to vote
1
Answered by
gitrog 326 Moderation Voter
5 years ago

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.

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 :) kieranhendy 28 — 5y
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 User#22604 1 — 5y
0
it'll work either way though User#22604 1 — 5y
0
Yeah you can connect it to the character, but then it's firing a lot more events. gitrog 326 — 5y
Ad

Answer this question