What I did last time to teleport players to the minigame after the intermission was get a teleporter, make CanCollide false and make Anchored false. The teleporter fell down on the players and teleported them to the minigame. It does work, but not that well. So how do I do it?
Let's start out with a freemodel teleporter script and see how it works:
--[[ INSTRUCTIONS: Place both teleporter1a and teleporter1b in the same directory (e.g. both in workspace directly, or both directly in the same group or model) Otherwise it wont work. Once youve done that, jump on the teleporter to teleport to the other. If you want more than one set of teleporters I will be adding more types in the future. Send me requests and ideas - miked. --]] --Enter the name of the model you want to go to here. ------------------------------------ modelname="teleporter8d" ------------------------------------ function onTouched(part) if part.Parent ~= nil then local h = part.Parent:findFirstChild("Humanoid") if h~=nil then local teleportfrom=script.Parent.Enabled.Value if teleportfrom~=0 then if h==humanoid then return end local teleportto=script.Parent.Parent:findFirstChild(modelname) if teleportto~=nil then local torso = h.Parent.Torso local location = {teleportto.Position} local i = 1 local x = location[i].x local y = location[i].y local z = location[i].z x = x + math.random(-1, 1) z = z + math.random(-1, 1) y = y + math.random(2, 3) local cf = torso.CFrame local lx = 0 local ly = y local lz = 0 script.Parent.Enabled.Value=0 teleportto.Enabled.Value=0 torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz)) wait(3) script.Parent.Enabled.Value=1 teleportto.Enabled.Value=1 else print("Could not find teleporter!") end end end end end script.Parent.Touched:connect(onTouched)
Now, please read through the script again, but with my annotations:
--[[ INSTRUCTIONS: Place both teleporter1a and teleporter1b in the same directory (e.g. both in workspace directly, or both directly in the same group or model) Otherwise it wont work. Once youve done that, jump on the teleporter to teleport to the other. If you want more than one set of teleporters I will be adding more types in the future. Send me requests and ideas - miked. --]] --Enter the name of the model you want to go to here. ------------------------------------ modelname="teleporter8d" ------------------------------------ function onTouched(part) if part.Parent ~= nil then local h = part.Parent:findFirstChild("Humanoid") --Assuming this is a character, find the Humanoid if h~=nil then local teleportfrom=script.Parent.Enabled.Value if teleportfrom~=0 then if h==humanoid then return end local teleportto=script.Parent.Parent:findFirstChild(modelname) --Reference the brick we are teleporting to if teleportto~=nil then local torso = h.Parent.Torso --Reference Character's Torso local location = {teleportto.Position} --Save position of the brick we are teleporting to local i = 1 --(location[i] is referencing the only thing in the table, the position. Don't worry if this confuses you, because it doesn't make sense.) local x = location[i].x --Saving the x coordinate of the position local y = location[i].y -- y coordinate local z = location[i].z --z coordinate x = x + math.random(-1, 1) --Offsetting the coordinates by a random amount for each axis z = z + math.random(-1, 1) y = y + math.random(2, 3) local cf = torso.CFrame local lx = 0 local ly = y local lz = 0 script.Parent.Enabled.Value=0 teleportto.Enabled.Value=0 torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz)) -- Move Torso to teleportTo position with the offsets wait(3) script.Parent.Enabled.Value=1 teleportto.Enabled.Value=1 else print("Could not find teleporter!") end end end end end script.Parent.Touched:connect(onTouched)
Now that we know how the freemodel script works, let's know exactly what we need to script.
Our script will: Teleport all players to a minigame.
In order to do so, we must: 1) Find all players in the game 2) Teleport them to the minigame
The teleport pads don't have any need to find all the players in the game. So I am going to have you use an "in pairs" loop.
Please look at the in pairs script and output shown. For this particular script, order we get the players does not matter so we do not need to use ipairs.
--This script satisfies the first part of our script. Getting all the Players in the game local Players = game:GetService("Players") for index, player in pairs(Players:GetChildren()) do print("We found "..player.." is in this server!") end
Once we find all players, we need to "Teleport them to the minigame." Let's take a look at our free model again.
More or less, it teleports the player to local location = {teleportto.Position}
by means of torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz))
To make things less complex, let's say you do torso.CFrame = targetBrick.CFrame
--This script satisfies the first part of our script. Getting all the Players in the game local Players = game:GetService("Players") for index, player in pairs(Players:GetChildren()) do print("We found "..player.." is in this server!") --Next we teleport the player, by changing the Torso's CFrame torso.CFrame = targetBrick.CFrame end
Now we have our basic script down! But the script still doesn't know where to find 'torso' in the script we just made, as it never got defined. We found the players inside game.Players
but we never actually connected that to the torso.
The easy way to connect the Player in the Players Service is to access a property of the Player called Character.
In other words, game.Players.PLAYERNAME.Character
can be used to reference the Character. Let's use this newfound knowledge in our script!
--This script satisfies the first part of our script. Getting all the Players in the game local Players = game:GetService("Players") for index, player in pairs(Players:GetChildren()) do print("We found "..player.." is in this server!") --Next we teleport the player, by changing the Torso's CFrame local Character = player.Character --find the Player's Character by looking at the property called Character --Next, let's define torso since we know where the character is now local torso = Character.Torso torso.CFrame = targetBrick.CFrame end
Alright, now we have found all the players, found all of the players' torsos, and we are telling the script to change the Torso's CFrame to the CFrame of targetBrick
to teleport the player.
However, we still need to define targetBrick
in the script. You can put a brick where you want the players to spawn and turn cancollide off, and reference it in the script as local targetBrick = game.Workspace.TARGETBRICKPARTNAME
. Alternatively, you can put the position in manually, yourself.
local Players = game:GetService("Players") for index, player in pairs(Players:GetChildren()) do print("We found "..player.." is in this server!") --Next we teleport the player, by changing the Torso's CFrame local Character = player.Character --find the Player's Character by looking at the property called Character --Next, let's define torso since we know where the character is now local torso = Character.Torso torso.CFrame = CFrame.new(0, 0, 0) --Put the position here end