Teleporting GUI Button

I’m trying to make it so when a gui button is clicked, the screen will flash for a bit and behind the flash will be your avatar teleporting to a brick and after a period of time they get teleported back to the exact spot they were at and the flash will disappear. Any ideas on how to achieve this?

2 Likes

When the button is clicked, save the current position, when make a really white (I suppose) frame and make it Visible = true, wait around 1 second then teleport the player using HumanoidRootPart’s CFrame to the CFrame location you want (Part.CFrame), wait 1 more second or so, set the visible property of the frame to false, then wait the time you want and set the HumanoidRootPart’s CFrame to the stored location.

Try this. Note that this was made with some assumptions in mind:

  • The script is a local script
  • The script is parented to the button that activates the script
  • The screen cover that makes the flash is a sibling of the button (they share a parent)
  • Frame properties:

Visual of their relationships
image

You can change all these things just make sure that you account for the changes.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local rootPart = character:WaitForChild("HumanoidRootPart")

local button = script.Parent
local frame = button.Parent.Frame --the screen cover that will make the "flash" effect

local Time
local waitTime = 10 --replace this with amount of time the player is teleported away

local active = false

local function teleportPlayer()
	
	frame.BackgroundTransparency = 0
	button.BackgroundTransparency = .5 --A visual effect to show the button isn't pressable
	
	rootPart.CFrame = game.Workspace.ButtonPart.CFrame * CFrame.new(0,rootPart.Size.Y,0)
	
	wait(1)
	
	button.Parent.Frame.BackgroundTransparency = 1
end

local function teleportBack(originCF)
	
	frame.BackgroundTransparency = 0
	button.BackgroundTransparency = 0
	
	rootPart.CFrame = originCF
	
	wait(1)
	
	active = false --Sets it so that the button can be pressed again
	frame.BackgroundTransparency = 1
end

button.Activated:connect(function()
	if not active then active = true --If the button has been pressed, it can't be pressed again
		
		local originCF = rootPart.CFrame
		
		teleportPlayer()
		
		Time = os.time()
		repeat wait() until os.time() - Time >= waitTime
		
		teleportBack(originCF)
	end
end)

Let me know if you have any questions!

1 Like

Sorry for the really late reply as I wasn’t checking my notifs but I’ll try out this method when I can and get back to you!

1 Like

I got it working and modified it to my likings, thank you so much for this as it really helped me learn some things too!

1 Like

That’s great! Glad I could help :happy3:

What you’d probably have to do is :Clone() the model and then position it accordingly. It would probably look something like this:

local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local zOffset = 5 --Tweak this number, it's for in front and behind (relative to the character)

local newModel = game.ReplicatedStorage.Model:Clone()
newModel.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,0,zOffset)
newModel.Parent = HumanoidRootPart