Making models teleport to random locations?

Basically, I have a folder full of parts called “BlimpPos”. I want someone who spawns in a blimp to have their blimp teleport to one of the 9 locations randomly. How do I do this?

2 Likes

1.You can Weld them together and then move a Center part.

or

2.You can change from a Folder to a Model and set PrimaryPart to a Center part then use Model:SetPrimaryCFame()

I would do the 2nd method because Welding is a pain in the butt to deal with.

1 Like

I would also check if there aren’t any blimps in the spawn place prior to teleporting
one issue I came across is how you would prevent the players from spawning on top of other players which sit in the spawn place, but the easiest solution to implement in my opinion is to have a number of spawns equal or greater to the maximum number of players per server, so that one would be guaranteed to be free for the player at any time

1 Like

I’m assuming your folder “BlimpPos” is full of the possible positions that blimps can spawn at & that the blimp is a model w/ a PrimaryPart.
For pseudorandom number generation we’re going to take advantage of the “Random” API


First, let’s declare our variables

local spawnPositionsFolder = workspace.spawnPositionFolder-- Change this to point towards your folder
local possibleSpawnPositions = spawnPositionsFolder:GetChildren() 
local randomObject = Random.new() -- Create a new "Random" object.

Next, we'll declare a function we can call that'll randomly select one child from _spawnPositionsFolder _ and return it.
local function pickRandomSpawnPosition()
	-- Pick a pseudorandom number within the range of 1 - however many children your possibleSpawnPositions folder has
	local randomInt = randomObject:NextInteger(1, #possibleSpawnPositions)

	 -- Return the child in the "randomInt"-th index's CFrame. (i.e. if "randomInt" is five, will return the fifth entry in possibleSpawnPositions
	return possibleSpawnPositions[randomInt].CFrame
end

Finally, we'll declare a function that actually teleports a passed blimp to a pseudo-randomly selected position.
local function teleportBlimpToSpawnPosition(blimp)
	-- Select a random spawn pos
	local spawnPosition = pickRandomSpawnPosition()
	
	-- Note: This is assuming "blimp" is a model that has a primarypart.
	-- Set's the blimp's cframe to that of our randomly selected spawn position + an offset of the blimp's height divided by two
	-- We offset it so that it doesn't spawn inside of the BlimpPos part.
	blimp:SetPrimaryPartCFrame( CFrame.new(spawnPosition.X, spawnPosition.Y + blimp:GetExtentsSize().Y/2, spawnPosition.Z) )
end

Final Product

2018-07-29_06-42-30

Code Blocks Compiled
local spawnPositionsFolder = workspace.spawnPositionFolder-- Change this to point towards your folder
local possibleSpawnPositions = spawnPositionsFolder:GetChildren() 
local randomObject = Random.new() -- Create a new "Random" object.

local function pickRandomSpawnPosition()
	-- Pick a pseudorandom number within the range of 1 - however many children your possibleSpawnPositions folder has
	local randomInt = randomObject:NextInteger(1, #possibleSpawnPositions)

	 -- Return the child in the "randomInt"-th index's CFrame. (i.e. if "randomInt" is five, will return the fifth entry in possibleSpawnPositions
	return possibleSpawnPositions[randomInt].CFrame
end

local function teleportBlimpToSpawnPosition(blimp)
	-- Select a random spawn pos
	local spawnPosition = pickRandomSpawnPosition()
	
	-- Note: This is assuming "blimp" is a model that has a primarypart.
	-- Set's the blimp's cframe to that of our randomly selected spawn position + an offset of the blimp's height divided by two
	-- We offset it so that it doesn't spawn inside of the BlimpPos part.
	blimp:SetPrimaryPartCFrame( CFrame.new(spawnPosition.X, spawnPosition.Y + blimp:GetExtentsSize().Y/2, spawnPosition.Z) )
end
1 Like

This joined the original blimp and the cloned blimp together for some reason

Mind elaborating on what you mean by that?

I have some blimps hidden in the workspace where players can’t get to and when a blimp is cloned it originates from one of those blimps (depending on what type of blimp). The blimp cloned but it was moved to the blimp it was cloned from and they both joined together while also moving together.

  local events = game.ReplicatedStorage.RemoteEvents
local spawns = game.Workspace.Everything.BlimpSpawns
local redspawns = spawns.Red:GetChildren()
local randomObject = Random.new()



events.CloneBlimp.OnServerEvent:connect(function(player, blimp, parent)
	local clone = blimp:Clone()
	
	clone.Parent = game.Workspace
	if player.TeamColor.Name == "Bright red" then
		
		local function pickRandomSpawnPosition()
			
	
		local randomInt = randomObject:NextInteger(1, #redspawns)
	
	return redspawns[randomInt].CFrame
	
	end
	
	local function teleportBlimpToSpawnPosition(blimp)
		
		local spawnPosition = pickRandomSpawnPosition()
		

	end
	
	local char = game.Workspace:FindFirstChild(player.Name)
	char.Torso.CFrame = CFrame.new(clone.person299.Position)
	clone.Name = player.Name.."Blimp"
	
	else
		clone:MoveTo(game.Workspace.Everything.BlimpSpawns.Blue.BlimpPos.Position)
	local char = game.Workspace:FindFirstChild(player.Name)
	char.Torso.CFrame = CFrame.new(clone.person299.Position)
	clone.Name = player.Name.."Blimp"
		
	end
end)

Would you mind embedding your code into your message, rather than having it as a screenshot?

Sure.

Try this
local events = game.ReplicatedStorage.RemoteEvents
local spawns = game.Workspace.Everything.BlimpSpawns
local randomObject = Random.new()

local function pickRandomSpawnPosition(possibleSpawnPositions)
	-- Pick a pseudorandom number within the range of 1 - however many children your possibleSpawnPositions folder has
	local randomInt = randomObject:NextInteger(1, #possibleSpawnPositions)

	 -- Return the child in the "randomInt"-th index's CFrame. (i.e. if "randomInt" is five, will return the fifth entry in possibleSpawnPositions
	return possibleSpawnPositions[randomInt].CFrame
end

local function teleportBlimpToSpawnPosition(blimp, possibleSpawnPositions)
	-- Select a random spawn pos
	local spawnPosition = pickRandomSpawnPosition(possibleSpawnPositions)
	
	-- Note: This is assuming "blimp" is a model that has a primarypart.
	-- Set's the blimp's cframe to that of our randomly selected spawn position + an offset of the blimp's height divided by two
	-- We offset it so that it doesn't spawn inside of the BlimpPos part.
	blimp:SetPrimaryPartCFrame( CFrame.new(spawnPosition.X, spawnPosition.Y + blimp:GetExtentsSize().Y/2, spawnPosition.Z) )
	-- Set the blimp's parent
	blimp.Parent = workspace
end


events.CloneBlimp.OnServerEvent:connect(function(player, blimp)
	local clone = blimp:Clone()
	
	local team = player.TeamColor == BrickColor.new("Bright red") and "Red" or "Blue"
	teleportBlimpToSpawnPosition(clone, spawns[team]:GetChildren())
	
	player.Character.HumanoidRootPart.CFrame = clone.person299.CFrame
	clone.Name = player.Name.." Blimp"
end)
4 Likes

image

Woops! Edited.