I made a script that was supossed that when you touch a part in-game, an UI pops out and when you click okay on the UI, you would get teleported to a CFrame.
Construction of the code, etcetera.
The phisic model of the structure and how it looks.
How the UI is made, and how it visually looks.
The LUA code.
local Pad1 = script.Parent.TeleportPad1
local Pad2 = script.Parent.TeleportPad2
local TPGUI = game.ServerStorage.TeleportationGUI
Pad1.Touched:Connect(function(hit)
TPGUI.Parent = game.StarterGui
TPGUI.Frame.TextButton.MouseButton1Click:Connect(function()
if hit.Parent:FindFirstChild('humanoid') then
hit.Parent.UpperTorso.CFrame = CFrame.new(-986, 4.5, -1040)
wait(0.4)
TPGUI.Parent = game.ServerStorage
end
end)
end)
So I first made the local variables. Then I started with a âwhen body touchedâ function on hit, that moved the actual GUI parent to the StarterGUIs so itâs visible. Then if the player clicked on âOkieâ, it connected with another function, that conditioned that if the player hits, it should find the first child called âhumanoidâ, and, then that the torso gets to the stated CFrame, it waits 0.4, and puts the parents of the GUI again the ServerStorage.
Youâre setting the parent to game.StarterGui! Players cannot see StarterGui You have to parent it to the playerâs PlayerGui folder for them to see it.
local Pad1 = script.Parent.TeleportPad1
local Pad2 = script.Parent.TeleportPad2
local TPGUI = game.ServerStorage.TeleportationGUI
Pad1.Touched:Connect(function(hit, player)
if hit.Parent:FindFirstChild('Humannoid') then
TpGui:Clone().Parent = player.PlayerGui
end
end)
end)
Then add a server script into the UI button and type the teleport code.
Well it depends, if itâs a server script doing the cloning then no because that can access server storage but if itâs a local script, which if in workspace would be surprising/not considered best practice then yes the gui would have to be moved to replicated storage. Just to be clear if it is a local script then change it as it will not work.
There is no second parameter for the Touched event, itâs only the Part that it HIt
If you want to get the Player, you have to refer to the Playerâs Character by using the GetPlayerFromCharacter() function
local Pad1 = script.Parent.TeleportPad1
local Pad2 = script.Parent.TeleportPad2
local TPGUI = game.ServerStorage.TeleportationGUI
local PlayerDB = {}
Pad1.Touched:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and not table.find(PlayerDB, Player) then
table.insert(PlayerDB, Player)
TPGUI:Clone().Parent = Player.PlayerGui
end
end)
It would be a bad idea to take the GUI instead of cloning it
local Pad1 = script.Parent.TeleportPad1
local Pad2 = script.Parent.TeleportPad2
local TPGUI = game.ServerStorage.TeleportationGUI
Pad1.Touched:Connect(function(hit)
local clonedGui = TPGUI:Clone() -- Clone it so it doesnt appear as nil if someone else is trying to use it
clonedGui.Parent = game.StarterGui
clonedGui.Frame.TextButton.MouseButton1Click:Connect(function()
if hit.Parent:FindFirstChild('humanoid') then
hit.Parent.UpperTorso.CFrame = CFrame.new(-986, 4.5, -1040)
wait(0.4)
clonedGui:Destroy()
end
end)
end)