I’ve newly opened Roblox commissions and my first job is to make a teleporter with GUI.
I have server scripts inside the teleporter parts and a local script in the GUI. I want to make the server script recognise when the parent part is touched and make the GUI visible. Afterwards, the player can choose which part he wants to teleport to.
The problem is that the part isn’t registering the player’s touch.
Server script inside part:
local teleportPart = script.Parent
local player = game:GetService("Players").LocalPlayer
local touchEvent = game:GetService("ReplicatedStorage").TouchEvent
teleportPart.Touched:Connect(function(hit)
if hit == player then
touchEvent:FireClient()
print("touched")
end
end)
teleportPart.TouchEnded:Connect(function(hit)
if hit == player then
touchEvent:FireClient()
print("touch ended")
end
end)
Local script inside GUI:
local frame = script.Parent.Frame
local touchEvent = game:GetService("ReplicatedStorage").TouchEvent
touchEvent.OnClientEvent:Connect(function()
frame.Visible = not frame.Visible
end)
for _, button in pairs(frame:GetChildren()) do
if button:IsA("TextButton") then
button.MouseButton1Click:Connect(function()
local location = button.Location.Value
local char = game:GetService("Players").LocalPlayer.Character
if char then
char.HumanoidRootPart.CFrame = location.CFrame + Vector3.new(5,5,0)
end
end)
end
end
You cant get the local player inside of a server script. Instead, see if the hit part belongs to a player and then do the teleportin. Also you need to give the fireclient method the name of the player you’re firing to. Here’s how:
local teleportPart = script.Parent
local touchEvent = game:GetService("ReplicatedStorage").TouchEvent
teleportPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
touchEvent:FireClient(player)
print("touched")
end
end)
teleportPart.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
touchEvent:FireClient(player)
print("touched ended")
end
end)
You can’t get LocalPlayer on the server script because there can be many players or clients connected to the server, but you can on the client cuz LocalPlayer refers to the Player-related to that client itself.
Here hit refers to the BasePart that the teleportPart part touches, as it is of class BasePart, there ain’t no possible way to compare using = (equals to) result in a true ever.
Also you cant fire to a client without specifying a client.
Well to get the player, you can do this:
teleportPart.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
local hrp = hit.Parent:FindFirstChild("HumanoidRootPart")
if(hum and hrp) then
hrp.CFrame = <Where Ever You wanna tp>
end
end)