Random Place Teleporter with an imagebutton

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    An image button that teleports you to a random game

  2. What is the issue? Include screenshots / videos if possible!
    idk how to code it, i tried to see post here

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yea
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Here is how to make that system!


Here are all the steps!

a) Create the ImageLabel
b) Create a LocalScript inside the ImageLabel
c) Write the script!


I’ll skip the first 2 steps I think you can do it!

Let’s make the script.


Firstly, we need to determine the services:

local MPS = game:GetService("MarketplaceService") -- We use MarketplaceService to get a Dictionary with all the information from an id
local TS = game:GetService("TeleportService") -- We use TeleportSerivce to actually teleport the player to a game/place

Now, we should write how we will know that the button is clicked.

script.Parent.MouseButton1Click:Connect(function()
end)

When the ImageLabel is clicked it will fire a function.


To get a random game id we will use math.random.

	local id = math.random(1, 100000000)

But how will we check if the id that has been generated is actually a game Id? For this reason, we use MPS (MarketplaceService ) to get a dictionary (let’s say a list ) with some information that will help us check what the Id is.

To ensure we actually get an id we will put the “check” inside a pcall. (Ensuring that we can handle any errors )

local PlaceCheck
	local success, whoops = pcall(function()
		PlaceCheck = MPS:GetProductInfo(tonumber(id))
	end)

Lastly, we need to check if the pcall returns success (so the id is actually a game id ). If it does, it will teleport the player there!

if success and PlaceCheck and PlaceCheck.AssetTypeId == 9 then
		TS:Teleport(tonumber(id), game.Players.LocalPlayer)
	else
		id = math.random(1, 100000000)
	end

*You can find more about AssetTypes here!.


Final Code
local MPS = game:GetService("MarketplaceService")
local TS = game:GetService("TeleportService")


script.Parent.MouseButton1Click:Connect(function()
	local id = math.random(1, 100000000)
	local PlaceCheck
	local success, whoops = pcall(function()
		PlaceCheck = MPS:GetProductInfo(tonumber(id))
	end)

	if success and PlaceCheck and PlaceCheck.AssetTypeId == 9 then
		TS:Teleport(tonumber(id), game.Players.LocalPlayer)
	else
		id = math.random(1, 100000000)
	end
end)

This text will be hidden


Hope this helped you, if you have any other questions let me know! :+1:

Thanks you so much, this helped me alot !

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.