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

How would you make this Gui teleport to more than one of the same part?

Asked by 9 years ago

I have multiple parts named "MainTeleportPart" in the Workspace that I want to be able to teleport to, but it only teleports to one of them until the server restarts, then it's a different one over again. How would I be able to do that?

script.Parent.MouseButton1Down:connect(function()
    script.Parent.Parent.Parent.Parent.Character.Torso.CFrame = game.Workspace.MainTeleportPart.CFrame
    if script.Parent.Parent.Parent.Parent.Character:findFirstChild("ForceField") then
        script.Parent.Parent.Parent.Parent.Character["ForceField"]:remove()
    end
end)

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

To do this you need to get a table of all the parts named 'MainTeleportPart' in workspace. Then use math.random for indexing the table. Also, use 'game.Players.LocalPlayer' in a localscript to access the player.

Localscript;

local spawns = {}
local plr = game.Players.LocalPlayer

function logParts(model)
    for i,v in pairs(model:GetChildren()) do
        if v:IsA('BasePart') and v.Name == 'MainTeleportPart' then
            table.insert(spawns,v)
        end
        logParts(v)
    end
end

logParts(workspace);

script.Parent.MouseButton1Down:connect(function()
    local spawn = spawns[math.random(1,#spawns)]
    plr.Character.Torso.CFrame = spawn.CFrame * CFrame.new(0,3,0)
    for i,v in pairs(plr.Character:GetChildren()) do
        if v:IsA('ForceField') then v:Destroy() end
    end
end)
0
Works, thanks! Pawsability 65 — 9y
Ad

Answer this question