TextButton.Activated not working

Hello!

So, I’ve been trying to make this script where when a player clicks on a TextButton, they are “teleported” via CFrame to a new position.

Essentially, players can press on a “TP to obby” button that teleports them to an obby. When players click on the TP button, they land on a specific part. As long as players stand on that specific part, a TextButton and a TextLabel are visible. The only way to close the TextButton and TextLabel is by pressing the TextButton, but I’d like players to also be teleported to a new position when they press the TextButton.

Here’s my issue: I made a Local Script under StarterGui, and it doesn’t work. No errors in the output. I’m not really sure what to do.
Here’s the script:

local player = game.Players.LocalPlayer
local tphere = game.Workspace.level1.tptohere.TPPos

--buttons--
local tpbutton = player.PlayerGui.ScreenGui.TextButton


tpbutton.Activated:Connect(function()
	player.Character.LowerTorso.CFrame = CFrame.new(tphere.Position)
end)

I’ve tried by using MouseButton1Click instead of .Activated, but it didn’t change nothing. It just doesn’t want to TP the LocalPlayer.

Thanks very much!

4 Likes

If you add a print to your Activated function can you see it in the output? Basically, does the activated get triggered or not?

1 Like
local player = game.Players.LocalPlayer
local tphere = game.Workspace.level1.tptohere.TPPos

--buttons--
local tpbutton = player.PlayerGui.ScreenGui.TextButton


tpbutton.Activated:Connect(function()
	player.Character.LowerTorso.CFrame = CFrame.new(tphere.Position)
	print("TP Worked")
end)

Updated script^
When I press on the “close” button, nothing prints.

1 Like

Can you show where the button is in the Explorer with a screenshot? It is a bit weird because your code should work.

1 Like

StarterGui

Capture d’écran, le 2020-07-13 à 13.02.49

1 Like

Well, ok but where is the TextButton?

EDIT: Also do me a favor and add a print at the beginning of the script to make sure the scripts runs. I know local scripts should run from inside PlayerGui, but just to be sure. Maybe they need to be inside a ScreenGui or something.

1 Like

StarterGui > ScreenGui > TextButton

Capture d’écran, le 2020-07-13 à 13.08.46

Updated script (with the print)

print("This is inside PlayerGui")

local player = game.Players.LocalPlayer

local tphere = game.Workspace.level1.tptohere.TPPos

--buttons--

local tpbutton = player.PlayerGui.ScreenGui.TextButton

tpbutton.Activated:Connect(function()

player.Character.LowerTorso.CFrame = CFrame.new(tphere.Position)

print("TP Worked")

end)

The message prints successfully.

1 Like

Should I report this as a bug?

1 Like

What script is it? There are multiple scripts I see in the image you provided.

1 Like

The one called “AutomaticTP”

All the rest are related to the close button and making the GUI visible when a player stands on a specific part.

1 Like

May I ask why not just put that script inside of the ScreenGui?

1 Like

I’ve tried that, but it doesn’t change anything.

1 Like

Try changing this:

tpbutton.Activated:Connect(function()
	player.Character.LowerTorso.CFrame = CFrame.new(tphere.Position)
end)

To this:

tpbutton.MouseButton1Click:Connect(function()
	player.Character.HumanoidRootPart.CFrame = tphere.CFrame
end)

Apologies if this isn’t the solution.

3 Likes

It seems like you are setting the player’s CFrame with a Vector3 position. This shouldn’t be the case. You need to do:

player.Character:SetPrimaryPartCFrame(tphere.CFrame)
1 Like

I’ve tried both, but it still doesn’t work:

print("This is inside PlayerGui")
local player = game.Players.LocalPlayer
local tphere = game.Workspace.level1.tptohere.TPPos

--buttons--
local tpbutton = player.PlayerGui.ScreenGui.TextButton


tpbutton.Activated:Connect(function()
	player.Character:SetPrimaryPartCFrame(tphere.CFrame)
	print ("TP worked")
end)

and

print("This is inside PlayerGui")
local player = game.Players.LocalPlayer
local tphere = game.Workspace.level1.tptohere.TPPos

--buttons--
local tpbutton = player.PlayerGui.ScreenGui.TextButton


tpbutton.MouseButton1Click:Connect(function()
	player.Character.HumanoidRootPart.CFrame = tphere.CFrame
	print ("TP worked")
end)

For some reason, none of them work.

1 Like

Maybe also try to put the script inside the ScreenGUI with the variable of tpbutton to:

local tpbutton = script.Parent:WaitForChild("TextButton")
1 Like

I think you added a space between ‘print’ and '(“TP worked”`).
print ("TP worked")

Make sure it is:
print("TP worked")

EDIT: Nevermind, sorry. It will still print with a space.

1 Like

It works! With this script:

print("This is inside PlayerGui")
local player = game.Players.LocalPlayer
local tphere = game.Workspace.level1.tptohere.TPPos

--buttons--
local tpbutton = script.Parent:WaitForChild("TextButton")


tpbutton.MouseButton1Click:Connect(function()
	player.Character.HumanoidRootPart.CFrame = tphere.CFrame
	print ("TP worked")
end)

However, upon testing it works 1/5. Should I add wait() or something similar so it works all the time?

1 Like

What do you mean by “it works 1/5”?

1 Like

I have to click “tpbutton” 5 times before it actually teleports me to the desired location.

1 Like