I made a GUI and put a local script in it, which is supposed to teleport the player when it clicks on it, however, the button isn't detecting clicks. Please can you tell me what I have done wrong, thanks in advance.
01 | local player = game.Players.LocalPlayer |
02 | local playerGui = player:WaitForChild( "PlayerGui" ) |
03 | local deployScreen = playerGui.DeployScreen |
04 | local target = game.Workspace.TeleportPart |
05 |
06 | script.Parent.MouseButton 1 Click:Connect( function () |
07 | print ( "Working" ) |
08 | player.Character.HumanoidRootPart.CFrame = target.CFrame * CFrame.new( 0 , 3 , 0 ) |
09 | deployScreen.Enabled = false |
10 | end ) |
Thanks in advance :).
You are attempting to use a local script to affect objects in the workspace. Instead, use your local script to trigger a remote event located in Replicated Storage in order to teleport the player with a script located in Server Script Storage.
Insert a RemoteEvent named teleportTrigger in Replicated Storage
Here is an example of your local script:
01 | local Players = game:GetService(“Players”) |
02 | local player = Players.LocalPlayer |
03 |
04 | local RepStorage = game:GetService(“ReplicatedStorage”) |
05 | local trigger = RepStorage:FindFirstChild(“teleportTrigger”) |
06 |
07 | —I don’t know where your button and gui instances are located so this is my best guess. |
08 | script.Parent.MouseButton 1 Click:connect( function () |
09 | print (“Working...”) |
10 | trigger:FireServer() |
11 | script.Parent.Parent:Destroy() |
12 | end ) |
Here is the script which is located in Server Script Storage:
1 | local RepStorage = game:GetService(“Replicated Storage”) |
2 | local trigger = RepStorage:FindFirstChild(“teleportTrigger”) |
3 |
4 | local target = game.Workspace:WaitForChild(“teleportPart”) |
5 |
6 | trigger.OnServerEvent:connect( function (player) |
7 | game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.CFrame = target.CFrame * CFrame.new( 0 , 3 , 0 ) |
8 | end ) |
This should work make sure you put the right codes in the right scripts. I also wrote this all on my phone(took me a while) so I hope I helped.
Do not use a local script because it will be harder to manipulate objects in workspace.
01 | game.players.playerAdded:Connect( function (playertogetgui) |
02 | local playerGui = player:WaitForChild( "PlayerGui" ) |
03 | local deployScreen = playerGui.DeployScreen |
04 | local target = game.Workspace.TeleportPart |
05 |
06 | end |
07 | script.Parent.MouseButton 1 Up:Connect( function (player) |
08 | print ( "Working" ) |
09 | player.Character.HumanoidRootPart.Position = target.Postion + Vector 3. new( 0 , 3 , 0 ) |
10 | deployScreen.Enabled = false |
11 | end ) |
Put this in ServerScriptService. Note: I'm using Vector3 because I'm terrible at CFrame