Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Converting A Teleport Script Into A Clickable GUI?

I have this old teleport script that I converted to work with R15 so it works with my new game. And I want to convert the script so when you click the GUI that is on the wall it teleports you. But I dont know how id go about this, here is the script.

-- Model it teleports to
------------------------------------
modelname="tele4"
------------------------------------

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.UpperTorso
					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)

UPDATE - Okay so I put the SurfaceGUI under StarterGUI and I put the script under TextButton. So it goes like this - StarterGUI.SurfaceGUI.TextButton.TeleportScript I dont know if I should move the teleport script up 1 so its directly under SurfaceGUI or what.

Heres the script so you know what your looking at

I should also mention its a localscript at the moment, I dont know if I should change it to a default script or what?

local plr = game.Players.LocalPlayer --This is the client
local char = plr.Character or plr.CharacterAdded:Wait() --This is the character
local root = char:WaitForChild("HumanoidRootPart") --This is the HumanoidRootPart
local teleportTo = workspace.RightExitDoor --This is where you teleport to!
local guiButton = game.StarterGui.SurfaceGui.TextButton --This is the button you're clicking!

guiButton.MouseButton1Down:Connect(function() --MouseButton1Down event
	root.CFrame = teleportTo.CFrame * CFrame.new(0,3,0) --Set the root's CFrame to the teleportobject's CFrame, translate it up by 3 studs
end)

1 answer

✓ Accepted answer

The majority of that code is completely unnecessary :)

To make teleporting compatible with both R6 and R15 rigs, CFrame the HumanoidRootPart instead of the Torso - Torsos are split up into UpperTorso and LowerTorso objects on R15 rigs..

Also, the event for clicking GuiButton objects would be MouseButton1Down, MouseButton1Up, or MouseButton1Click. You're using a Touched event, which doesn't make sense because GuiButton objects do not support the Touched event.. seeing as there is no bounding box to hit.. it is a 2D object.

Edit this according to comments:

local plr = game.Players.LocalPlayer --This is the client
local char = plr.Character or plr.CharacterAdded:Wait() --This is the character
local root = char:WaitForChild("HumanoidRootPart") --This is the HumanoidRootPart


local teleportTo = workspace.TeleportationLocation --This is where you teleport to!
local guiButton = script.Parent --This is the button you're clicking!

guiButton.MouseButton1Down:Connect(function() --MouseButton1Down event
	--Set the root's CFrame to the teleportobject's CFrame, translate it up by 3 studs
	root.CFrame = teleportTo.CFrame * CFrame.new(0,3,0)
end)

15 comments

0

Is that script just the bare bones, meaning it wont work. I know Ill have to change some things obviously. But will it work if it do? Also where do you put the script.?

0

All you need is a MouseButton1Down event and when it's clicked use the MouseButton1Down paramter to get the player then do Player.Character.HumanoidRootPart.CFrame = CFrame.new(0,0,0) To teleport them.

0

@GottaHaveAFunTime It will work. And you put that in the button.

0

@Shadi432 The MouseButton1Down event doesn't return the player who clicked. You're thinking of the MouseClick event of ClickDetectors.

0

@Goulstem It doesnt seem to be working

0

@Goulstem I placed it under SurfaceGUI.TextButton and it doesnt seem to be working.

0

Hmm.. is the SurfaceGui parented to StarterGui? If it isn't, move it there and set it's Adornee property to the object you want the ui to appear on.

0

@Goulstem I updated the post ingeneral so look at the main post above and it has more info. Maybe that will help out a little bit. :)

0

Alright, the problem is how you define the guiButton variable - since the script is a direct descendant of the button, you can simply do: guiButton = script.Parent

0

It still doesnt work man. I have no clue why its not working.

0

WAIT! OMGSHHHH ITS WORKING YAYYYYY FINALLYYYYY..... I mean... Thank you so much for not just giving up and thanks for helping me out :)

0

No problem :) Happy developing!

0

Ive just got 1 more question. if there a way I could make it so they cant spam the teleport button so there is like a timer between click.

0

Mhmm. This question is getting quite filled up, though. Ask another and both of us can recieve more rep :D

0

Thats a good point. Sorry im just getting used to all this scripting, I usually Animate and build so its all new to me.

This discussion is preserved as it was. New answers, comments, and votes are closed.