Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i get the targeted player to teleport to me?

Asked by 5 years ago
Edited 5 years ago

how can i connect the target function and player function into the server script so the server can bring the other player to me here is my local script

local player = game.Players.LocalPlayer

player.Chatted:Connect(function(msg)

local target --this would be the player that we're gonna tp to, its just a varibale to make things cleaner.

if msg:lower():match("goto") then

if game.Players:FindFirstChild(msg:sub(6)) then

target = workspace:FindFirstChild(msg:sub(6))

print(target)

player.Character.HumanoidRootPart.CFrame = CFrame.new(target.Head.Position)

end

end

end)



local player = game.Players.LocalPlayer

player.Chatted:Connect(function(msg)

local target --this would be the player that we're gonna tp to, its just a varibale to make things cleaner.

if msg:lower():match("get") then

if game.Players:FindFirstChild(msg:sub(6)) then

target = workspace:FindFirstChild(msg:sub(6))

print(target)

game:GetService("ReplicatedStorage"):WaitForChild("playertp").FireServer(player, target)

end

end

end)

here is my server script

game.ReplicatedStorage:WaitForChild("playertp").OnServerEvent:Connect(function(player, target)

target.HumanoidRootPart.CFrame = CFrame.new(player.Head.Position)

end)

i get no errors from this in-game

0
the playertp in serverstorage is a remotefunction, i did also try it as a remoteevent with the exact same script Gameplayer365247v2 1055 — 5y
0
The remote event you fired from the local script has a different name from the one on the server script. Local scripts can't access ServerStorage, only server scripts can. Store the remote in ReplicatedStorage so both client and server can access it hellmatic 1523 — 5y
0
ye i fixed both of those, i missed the name part and forgot about the replicated storage but even tho that is fixed the script dont work. pls help Gameplayer365247v2 1055 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

There are a couple errors in each script that I can point out and fix for you.

Here's what your local script should look like:

local player = game.Players.LocalPlayer
local remoteEvent = game.ReplicatedStorage:WaitForChild("playertp")

player.Chatted:Connect(function(msg)
    if msg:lower():match("goto") then
        local targetPlayer = game.Players:FindFirstChild(msg:sub(6))

        if targetPlayer and targetPlayer.Character then
        --Checks if the player and character is there

            player.Character.HumanoidRootPart.CFrame =  CFrame.new(target.Head.Position)
        end
    elseif msg:lower():match("get") then

        local targetPlayer = game.Players:FindFirstChild(msg:sub(5))
        --should be :sub(5) instead of :sub(6)

           if targetPlayer and targetPlayer.Character then

               remoteEvent:FireServer(targetPlayer.Character)
              --When firing, only fire the parameters, not the player

           end
     end
end)

I made some edits so that the Chatted function isn't repeated twice. I also used a conditional statement to check whether or not the target is in the game, and whether his/her character is loaded.

Something to keep in mind is when you are firing a remote event to the server, you do NOT include player as a parameter. The server automatically receives it.

Here's what the server script should look like:

local remoteEvent = game.ReplicatedStorage:WaitForChild("playertp")

remoteEvent.OnServerEvent:Connect(function(player, target)

    target.HumanoidRootPart.CFrame = CFrame.new(player.Character.Head.Position)
    --You forgot to address the Character of the player

end)

Notice how the server automatically picks up who fired the remote event. It's the very first thing that it picks up in every :FireServer() case.

I hope this answers your question, let me know if you need any more details :)

0
thanks this works but the first part with me teleporting to them didnt work, so i used my own way there but the script fully works now with my first part and ur second part Gameplayer365247v2 1055 — 5y
Ad

Answer this question