Teleportation Gui

Hey guys, I’m trying to make a script… just for some info, there are two parts, when two players stand on one each, a ‘playButton’ Gui pops up and I want it so that when this ‘PlayButton’ is clicked, it teleports one player to ‘Team1’ part and the other player to ‘Team2’ part. Any ideas how I can do this? Heres my current script thats not working, ik its probably really bad, i’m new to scripting, Thanks :slight_smile:

local playClicked = game.StarterGui.SwordFightPlay.PlayButton.PlayButton2.startButton
local player = game.Players.LocalPlayer
local Team1 = game.Workspace.Team1
local Team2 = game.Workspace.Team2
local team1Full = false
local start1 = game.Workspace.start1
local start2 = game.Workspace.start2

function startClicked()
	if player.Touched:Connect(start1) then
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Team1.Position.X,Team1.Position.Y + 3,Team1.Position.Z)
		team1Full = true
	end
	if player.Touched:Connect(start2) then
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Team2.Position.X,Team2.Position.Y + 3,Team2.Position.Z)
	end
end

playClicked.MouseButton1Click:Connect(startClicked)

You need to put the Touched event on the part and not on the player. And you don’t have to put it in a if statement. So like:

local part = workspace.Part

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then
        print("Part has been touched by a player")
    end
end)
3 Likes

local playClicked = game.StarterGui.SwordFightPlay.PlayButton.PlayButton2.startButton
local players = game.Players
local local_player = players .LocalPlayer
local Team1 = game.Workspace.Team1
local Team2 = game.Workspace.Team2
local team1Full = false
local start1 = game.Workspace.start1
local start2 = game.Workspace.start2
local state

local function startClicked()
if not state then
return
end
local part
if state == “Team1” then
part = Team1
elseif state == “Team2” then
part = Team2
end
local_player.Character:SetPrimaryPartCFrame(part .CFrame*CFrame.new(0,3,0))
end

playClicked.MouseButton1Click:Connect(startClicked)

local function TouchedEvent(hit,name)
local player,inst
inst = hit:FindFirstAncestorWhichIsA(“Model”)
if not inst then
return
end
player = players:GetPlayerFromCharacter(inst)
if not player then
return
end
state = name
playClicked.Visible = true
end

start2.Touched:Connect(function(hit)
TouchedEvent(hit,“Team2”)
end)

start1.Touched:Connect(function(hit)
TouchedEvent(hit,“Team1”)
end)

1 Like

Hi, thank you very much for the response! I just tried the script and found that nothing happens when I click the play button, any ideas why that is?

local playClicked = game.StarterGui.SwordFightPlay.PlayButton.PlayButton2.startButton
local players = game.Players
local local_player = players.LocalPlayer
local Team1 = game.Workspace.Team1
local Team2 = game.Workspace.Team2
local team1Full = false
local start1 = game.Workspace.start1
local start2 = game.Workspace.start2
local state

local function startClicked()
	if not state then
		return
	end
	local part
	if state == "Team1" then
		part = Team1
	elseif state == "Team2" then
		part = "Team2"
	end
	local_player.Character:SetPrimaryPartCFrame(part .CFrame*CFrame.new(0,3,0))
end

playClicked.MouseButton1Click:Connect(startClicked)

local function TouchedEvent(hit,name)
	local player,inst
	inst = hit:FindFirstAncestorWhichIsA("Model")
	if not inst then
		return
	end
	player = players:GetPlayerFromCharacter(inst)
	if not player then
		return
	end
	state = name
	playClicked.Visible = true
end

start2.Touched:Connect(function(hit)
	TouchedEvent(hit,"Team2")
end)

start1.Touched:Connect(function(hit)
	TouchedEvent(hit,"Team1")
end)

add a number value to your Teams and squeeze a statement inside the touched event that compares those values

1 Like

If you have two guis for both parts, do you want only one player who touches the gui to teleport them both, or only teleports the player who clicked the button?

1 Like

I want it so that when one player clicks the Gui, it teleports them both to different parts inside the arena

1 Like

Oh hello friend

  1. First create a Part in the workspace where the player will be teleported to it

  2. Then create a ScreenGui with the name of: TeleportionUI and inside it put a TextButton with the name of: Teleport1

  3. Then create a LocalScript in StarterCharacterScripts, and enter the code below

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local HumanoidRootPart = LocalPlayer.Character:WaitForChild("HumanoidRootPart") -- HumanoidRootPart(CFrame)

local IsDebounce = false -- Time Debounce

UserInputService.InputBegan:Connect(function(Hit) -- Identify player input
	LocalPlayer.PlayerGui.TeleportionUI.Teleport1.MouseButton1Click:Connect(function(Hit)
		if not (IsDebounce) then
			IsDebounce = true	
			
			HumanoidRootPart.CFrame = workspace.Part.CFrame -- TeleportSystem
			
			task.wait(5) -- Time for the person to be able to teleport again
			IsDebounce = false
		end
	end)
end)
1 Like

its better to use if game.Players:GetPlayerFromCharacter(hit.Parent) then
to check if the part was hit by a player, because if there is ever a chance a pet or npc or something has a humanoid, but itsnt a player touches the part.

1 Like

Not sure if this helps, but this is the mental walkthrough I get when thinking of this…

two pads for players to stand on… have a server script that is checking to see if players are on the pads

if a pad is touched, the server script sends an event to the client who is on it to display the button in a ‘ready’ state

when a player sees the gui button and clicks on it, a local script (probably child of the button) instantly sets the button to a ‘waiting’ state, and sends an event to the server script saying the button was clicked

when the server gets the signal a button was clicked, it stores that input, such as … button1Clicked = true, and checks the state of the other button

if both buttons have been clicked, the server, sends an event to clear the buttons from both clients, then clears the stored button states, then it randomly, or by order of who clicked first, etc… assigns the team, then uses a :PivotTo to teleport them to their teams location.

1 Like

A simpler way is to make it like the people in the thread has been replying
Which is when the character touches the team part, they teleport to the designated part


But if you insist on having the gui playButton, it will be a bit more complicated
Involving Client to Server Communication
Right now, you’re referencing the button from StarterGui from the Server, which will not work

You will need LocalScript working on the touch event and the gui click
And send a Signal through RemoteEvent then the server receives it and teleport their character
Remote Functions and Events (developer.roblox.com)

1 Like

How come these teams are in the workspace? There is a service called “Teams”

1 Like

These are “parts” named Team1 and Team2

1 Like

Right! Ok then, maybe try changing the CFrame.new lines to this:

player.Character.HumanoidRootPart.CFrame = Team1.CFrame + Vector3.new(0,3,0)

Same with line 15 but change Team1 to Team2

1 Like

Hey, thanks for the response, someone previously helped me and gave me the script I posted above, I’m new to scripting so this is all very complicated to me but in the script there is only one line to teleport the player, so I’m not sure what to do…

The if statement requires an expression that evaluates to true which is not the case here and the Connect() method requires a callback function while you are passing a part. So if with that expression you are trying to check whether a player is on a part then either you use the GetPartsInPart() (and you add a little box over the part where the player must stand on, obviously make the box CanCollide false) which would bring to cleaner code OR you use a .Touched event on the part that the player must stand on and use variables to check whether or not there is someone on the part

An example using GetPartsInPart() and OverlapParams

local boxPart1 = --instance of the box part above the part the first player must stand on
local boxPart2 =  --instance of the box part above the part the second player must stand on

function teleportPlayers()
    local op = OverlapParams.new() --op stands for OverlapParams
    op.FilterDescendantsInstances = {} --FilterDescendantsInstances requires an array containing all the models' descendants that will be subject to the FilterType
    op.FilterType = Enum.RaycastFilterType.BlackList --Every BasePart in the game will be considered except those that are descendants of objects in the FilterDescendantsInstances array.
    --There are two more properties for OverlapParams but in this case they are not need and so will be left as default

    local player1 = nil
    local player2 = nil
    local partsInBox1 = game.Workspace:GetPartsInPart(boxPart1, op) --This will return an array containing all the parts inside the detection box
    local partsInBox2 = game.Workspace:GetPartsInPart(boxPart2, op) 

    for index, part in pairs(partsInBox1) do --loop through all the parts in the returned array
        --get the player from the part
        player1 = game.Players:GetPlayerFromCharacter(part.Parent) --part.Parent references the character model

        if player1 then
            break --stop looping as the player was found
        end
    end
    for index, part in pairs(partsInBox2) do --loop through all the parts in the returned array
        --check if a part belongs to a player
        player2 = game.Players:GetPlayerFromCharacter(part.Parent) --part.Parent references the character model
 
        if player2 then 
            break
        end
    end

    --check if both player were found in the parts
    if player1 and player2 then --check if both players were found, if one was not its value would be nil but for the same concept explained in the first paragraph the code in the if statement would not run. Basically nil ~= true
        --Teleport the players here. player1 and player2 are player instances so you can retrieve their character model by using player1.Character or player2.Character
    end
end

playButton.MouseButton1Click:Connect(teleportPlayers)