Teleportation GUI script

Hi, I’m trying to make a Teleportation GUI for my Clicker Game, but the script doesn’t work, here’s my script, (Note, I used ChatGPT for this script.)

-- Set the buy price and click requirement
local buyPrice = 100 -- Change this to the desired buy price
local clickRequirement = 100 -- Change this to the desired click requirement

-- Variable to keep track of whether button has been clicked
local buttonClicked = false

-- Function to handle "GO" button click
local function onGoClicked()
	-- Add your desired action here when "GO" button is clicked
	print("GO button clicked!")

	-- Get the player
	local player = game.Players.LocalPlayer

	-- Teleport the player to the desired location
	local teleportTo = Vector3.new(-53.99, 1.671, 15.125) -- Replace x, y, z with the actual coordinates of the desired teleport location
	player.Character.HumanoidRootPart.CFrame = CFrame.new(teleportTo)
end

-- Function to handle button click
local function onButtonClicked()
	-- Get the button
	local button = script.Parent

	-- Check if button has already been clicked
	if buttonClicked then
		return
	end

	-- Get the scrolling frame
	local scrollingFrame = button.Parent

	-- Get the frame
	local frame = scrollingFrame.Parent

	-- Get the screen GUI
	local screenGui = frame.Parent

	-- Get the player
	local player = game.Players.LocalPlayer

	-- Check if player has a 'leaderstats' object
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		-- Get the player's clicks from leaderstats
		local clicks = leaderstats:FindFirstChild("Clicks")

		-- Check if player has enough clicks to buy the location
		if clicks and clicks.Value >= buyPrice then
			-- Subtract the buy price from player's clicks
			clicks.Value = clicks.Value - buyPrice

			-- Change button text to "GO"
			button.Text = "GO"

			-- Set buttonClicked to true to prevent further actions
			buttonClicked = true

			-- Call the onGoClicked function
			onGoClicked()
		end
	end
end

-- Connect button click event to onButtonClicked function
script.Parent.MouseButton1Click:Connect(onButtonClicked)

There’s my script, can anybody help me?
(By the way, there’s no errors in output.)

4 Likes

Maybe instead of using Vector3, place a part where you want to teleport yourself, anchor and disable CanCollide, and use

HumanoidRootPart.Position = Part.Position
1 Like

Sorry, but that doesn’t work, I tried it and did all those steps.

Does the rest of the script work? Like substracting your clicks value from the price

I deleted that line when I realised there were many red lines under it.

Ok, could you send the edited version of the script?

yes, here’s the updated:

-- Set the buy price and click requirement
local buyPrice = 100 -- Change this to the desired buy price
local clickRequirement = 100 -- Change this to the desired click requirement

-- Variable to keep track of whether button has been clicked
local buttonClicked = false

-- Function to handle "GO" button click
local function onGoClicked()
	-- Add your desired action here when "GO" button is clicked
	print("GO button clicked!")

	-- Get the player
	local player = game.Players.LocalPlayer

	-- Teleport the player to the desired location
	HumanoidRootPart.Position = Part.Position -- Replace x, y, z with the actual coordinates of the desired teleport location
	player.Character.HumanoidRootPart.CFrame = CFrame.new(teleportTo)
end

-- Function to handle button click
local function onButtonClicked()
	-- Get the button
	local button = script.Parent

	-- Check if button has already been clicked
	if buttonClicked then
		return
	end

	-- Get the scrolling frame
	local scrollingFrame = button.Parent

	-- Get the frame
	local frame = scrollingFrame.Parent

	-- Get the screen GUI
	local screenGui = frame.Parent

	-- Get the player
	local player = game.Players.LocalPlayer

	-- Check if player has a 'leaderstats' object
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		-- Get the player's clicks from leaderstats
		local clicks = leaderstats:FindFirstChild("Clicks")

		-- Check if player has enough clicks to buy the location
		if clicks and clicks.Value >= buyPrice then
			-- Subtract the buy price from player's clicks
			clicks.Value = clicks.Value - buyPrice

			-- Change button text to "GO"
			button.Text = "GO"

			-- Set buttonClicked to true to prevent further actions
			buttonClicked = true

			-- Call the onGoClicked function
			onGoClicked()
		end
	end
end

-- Connect button click event to onButtonClicked function
script.Parent.MouseButton1Click:Connect(onButtonClicked)

Maybe try this

if clicks.Value >= buyPrice

instead of this

if clicks and clicks.Value >= buyPrice

I don’t really know what is the problem here. If that doesn’t work, tell me if the line with

clicks = clicks - buyPrice

does something.

sorry, but I don’t know where to put those lines in the script.

I changed it, try this, if it wont work tell me if the line i marked, if it does anything with text

It doesn’t work, and I get this in the Output: 18:59:42.743 Players.PowerArmor2020.PlayerGui.ScreenGui.Frame.ScrollingFrame.TextButton.LocalScript:17: attempt to index nil with ‘Position’ - Client - LocalScript:17

The Part on line 17 gotta have a variable to it.

local Part = workspace.Part -- the part you want to tp to

Also, the where you set humRootPart CFrame to teleportTo variable, you can delete it

assuming that this is a local script there is a problem.

	-- Check if player has enough clicks to buy the location
		if clicks and clicks.Value >= buyPrice then
			-- Subtract the buy price from player's clicks
			clicks.Value = clicks.Value - buyPrice
                 ...
        end

when you are talking a value away it should be done in the server, hence you would need to use remote events

It doesn’t work, And there’s no Output errors