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.
local player = game.Players.LocalPlayer local playerGui = player:WaitForChild("PlayerGui") local deployScreen = playerGui.DeployScreen local target = game.Workspace.TeleportPart script.Parent.MouseButton1Click:Connect(function() print("Working") player.Character.HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0,3,0) deployScreen.Enabled = false 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:
local Players = game:GetService(“Players”) local player = Players.LocalPlayer local RepStorage = game:GetService(“ReplicatedStorage”) local trigger = RepStorage:FindFirstChild(“teleportTrigger”) —I don’t know where your button and gui instances are located so this is my best guess. script.Parent.MouseButton1Click:connect(function() print(“Working...”) trigger:FireServer() script.Parent.Parent:Destroy() end)
Here is the script which is located in Server Script Storage:
local RepStorage = game:GetService(“Replicated Storage”) local trigger = RepStorage:FindFirstChild(“teleportTrigger”) local target = game.Workspace:WaitForChild(“teleportPart”) trigger.OnServerEvent:connect(function(player) game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0,3,0) 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.
game.players.playerAdded:Connect(function(playertogetgui) local playerGui = player:WaitForChild("PlayerGui") local deployScreen = playerGui.DeployScreen local target = game.Workspace.TeleportPart end script.Parent.MouseButton1Up:Connect(function(player) print("Working") player.Character.HumanoidRootPart.Position = target.Postion + Vector3.new(0,3,0) deployScreen.Enabled = false end)
Put this in ServerScriptService. Note: I'm using Vector3 because I'm terrible at CFrame