Teleport Script not working

I trying to make it so that if the player touches a Gui it teleports them to the place but the code isn’t working I am not getting any errors.

--Variables
local tele1 = script.Parent.Tele1
local debounce = false

--Functions

game.StarterGui.Ships.Holder.Button.MouseButton1Click:Connect(function(clicked)
	if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
		print("YES")
		local char = clicked.Parent
		local humanoidrootpart = char.HumanoidRootPart
		humanoidrootpart.Position = tele1.Position
		debounce = true
		wait(1)
		debounce = false
	end
end)
3 Likes

It is a Gui Button.
It is a Gui Button.

--Variables
local tele1 = script.Parent.Tele1
local debounce = false
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local shipUi = playergui:WaitForChild("Ships")
local Holder = shipUi:WaitForChild("Holder")
local Button = Holder:WaitForChild("Button")

--Functions

Button.MouseButton1Click:Connect(function(clicked)
	if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
		local char = clicked.Parent
		local humanoidrootpart = char.HumanoidRootPart
		humanoidrootpart.Position = tele1.Position
		debounce = true
		wait(1)
		debounce = false
	end
end)

Made this but still doesn’t work

Button.MouseButton1Click doesn’t give any parameters to its connected function, you can see the docs here: GuiButton | Documentation - Roblox Creator Hub

2 Likes

I use a parameter to MouseButton1Click to detect whether it has been clicked and it always worked.

There are no supplied arguments to the functions bound to the MouseButton1Click() event.

image

Source: GuiButton

Mhm thats odd, I remember adding a click parameter and adding an if statement to check whether it has been clicked and it worked, maybe it ignored that part because the localscript already knows that the player can only click it.

I made the Button the Parent of the script, now it detects when I press it, but the teleportation script is not working.

I am getting an error:

error

This is the code:

    --Variables
        local tele1 = game.Workspace.Map.TeleportShip.Tele1
        local debounce = false
        local Button = script.Parent

        --Functions

    Button.MouseButton1Click:Connect(function(clicked)
    	if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
    		local char = clicked.Parent
    		local humanoidrootpart = char.HumanoidRootPart
    		humanoidrootpart.Position = tele1.Position
    		debounce = true
    		wait(1)
    		debounce = false
    	end
    end)

Yes of course you are, because you are trying to use clicked.Parent, when clicked is nil!

clicked will always be nil because MouseButton1Click doesn’t supply the clicked parameter.


If you are using a LocalScript you can simply get the player which clicked on the button with game.Players.LocalPlayer. If you are using a server script, you cannot find which player clicked the button (if it is a screen gui).

If you are using a surface gui, you cannot get the player that clicked on the button unless the gui is a descendent of PlayerGui

1 Like

I made the click detecting part.
But I am stuck at teleporting.

I made it work you are a legend you helped me a lot ty.
You also helped me with Perlin Noise ty.

No problem, just remember to mark the reply that helped you as the solution if the solution is on this thread :slight_smile:

2 Likes

Why not? using a Remote Event allows you to get the Local Player, you can find out who clicked it.

that was probably a Click Detector then, it passes the player who clicked it as a parameter. The MouseButton1Click event fires when a player left clicks a Gui object, there’s no parameters passed.


@EHGTR1903 your code seems very redundant, there’s lots of unnecessary stuff you’re doing.

Code in this whole part won’t even run, because on the first line where you bind the event to a function, you pass a parameter.

Your code will work if you don’t try to pass a ‘clicked’ parameter in the function and instead do this


 local player = game:GetService("Players").LocalPlayer  

 Button.MouseButton1Click:Connect(function()
 --// this is happening locally, no need to pass a player object as a param.

 local char = player.Character or player.CharacterAdded:Wait()
 -- rest of your code

 end)

also remember that instead of checking whether debounce == false
you can simply do

 if not debounce then 
1 Like

Thank you for your answer. I will try this too.

--Variables
local tele1 = script.Parent.Tele1
local debounce = false

--Functions

game.StarterGui.Ships.Holder.Button.MouseButton1Click:Connect(function(clicked)
	if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
		print("YES")
		local char = clicked.Parent
		local humanoidrootpart = char.HumanoidRootPart
		humanoidrootpart.CFrame = tele1.CFrame
		debounce = true
		wait(1)
		debounce = false
	end
end)

This should work

You can try this

local debounce = false
local tele1 = script.Parent.Tele1

script.Parent.MouseButton1Click:Connect(function()
if debounce == false then debounce = true
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local humanoidrootpart = char.HumanoidRootPart
humanoidrootpart.CFrame = tele1.CFrame
wait(1)
debounce = false
end
end)

When the humanoidrootpart’s cframe moves, the whole character will be teleported

Yes you are right, my fault for not checking. It only works as I outlined with other models, not with the Player model.

1 Like

I think that you are using a server script, for guis, you should definitely use local script.