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