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

How to make player teleport in front instead of beside?

Asked by 3 years ago

Hello developers!

I made a working teleport command and currently it teleports you +3 away from the player on the X axis. But I want it to teleport the player right in front of the player who called the command. Like this:

https://ibb.co/DKKzL2p

And here is my script:

local function findplayer(name)

    print("started process")
    for i,v in pairs(game.Players:GetPlayers()) do
print("looping")
        local name1 = string.lower(v.Name)

        if string.sub(name1, 1, string.len(name)) == name then
            print("found player")
return v

end

    end
    end

















local function handler(message, plr)
    print("function")
    local prefix = ":"
    local spacer = " "
    local lower = string.lower(message)
    local args = string.split(lower, spacer)
    local cmd = string.split(args[1], prefix)
    if cmd[2] == "tp" then  
        print("tp")
        if args[2] and args[3] then


                print("cmd aloud")
            local char3 = game.Workspace[(findplayer(args[2])).Name]
                print(char3)
            local char4 = game.Workspace[(findplayer(args[3])).Name]
                print(char4)
            char3.HumanoidRootPart.CFrame = CFrame.new(char4.HumanoidRootPart.CFrame.X-3,char4.HumanoidRootPart.CFrame.Y,char4.HumanoidRootPart.CFrame.Z+3)
            print("done")
            end
end
end




game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(message)
        print("chatted")
        handler(message, plr)
    end)
end)

game.ReplicatedStorage.Get.OnServerEvent:Connect(function(plr, message) -- receives here
    print("received")
    handler(message, plr)
end)

Thank you in advance! :)

1 answer

Log in to vote
1
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

To position a part relative to another part with CFrames, you should use the :ToWorldSpace method.

In your case:

Change

char3.HumanoidRootPart.CFrame = CFrame.new(char4.HumanoidRootPart.CFrame.X-3,char4.HumanoidRootPart.CFrame.Y,char4.HumanoidRootPart.CFrame.Z+3)

to

char3.HumanoidRootPart.CFrame = char4.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0,0,-3))

To understand CFrame manipulation more, visit this page of the Roblox Developer Website

Ad

Answer this question