How can I change the players CFrame to a random parts CFrame in a folder

Im trying to make it so when the player presses the start button it teleports the player to a random part on the map but I cant figure it out.

this is the code i have so far:
local Plr = game.Players.LocalPlayer
local Hrp = Plr.Character:WaitForChild(“HumanoidRootPart”)

local Play = script.Parent
local Settings = script.Parent.Parent.Settings

local SpawnFolder = workspace:WaitForChild(“SpawnLocations”):GetDescendants()

Play.MouseButton1Click:Connect(function()
for _, Spawns in pairs (SpawnFolder) do

	if Spawns:IsA("Part") then
		-- I have no idea what to put here
	end
end

end)

1 Like

Try This

local Plr = game.Players.LocalPlayer
local Hrp = Plr.Character:WaitForChild(“HumanoidRootPart”)

local Play = script.Parent
local Settings = script.Parent.Parent.Settings

local SpawnFolder = workspace:WaitForChild(“SpawnLocations”):GetDescendants()

Play.MouseButton1Click:Connect(function()
Hrp.CFrame = SpawnFolder[math.random(1,#SpawnFolder)].CFrame + Vector3.new(0,3,0) --Adjust so players spawn on top of the parts.

end)

Edit: will only work if everything in the "SpawnLocations" folder is a part. You can change it from :GetDescendants() to :GetChildren() if the parts are not in any subfolders or groups.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.