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

Teleporting a player instead of Changing it's velocity?

Asked by 5 years ago

Instead of just making the player go really fast, I want to actually teleport a player based on their camera's lookVector. Maybe like 50 studs or so. All my code so far is in a local script inside of StarterGui. How would make this change? Any help is appreciated!

--Moving the Humanoid
    --Double Tap
enabled = true
Mouse.KeyDown:connect(function(key)
    if not enabled and clicked then
        return
    end
    if key == "w" and _G.Energy >= 25 then
    enabled = false

    local function onTap()
        if tick() - lastTapTime < 0.2 then
            print("double Click!")
            clicked = true
                --What happends when the user double taps
            player.Character.UpperTorso.Velocity = cam.CFrame.lookVector * Vector3.new(1,0,1) * 400 -- This is the part I want to change.
            Trail.Parent = Char.HumanoidRootPart
            local attachment0 = Instance.new("Attachment", Char.Head)
            attachment0.Name = "TrailAttachment0"
            local attachment1 = Instance.new("Attachment", Char.HumanoidRootPart)
            attachment1.Name = "TrailAttachment1"
            Trail.Attachment0 = attachment0
            Trail.Attachment1 = attachment1
            wait(0.3)
            Trail:Remove()
          --  _G.Energy = _G.Energy - 25
            wait(0.5)  
            end
        end

        onTap()
        lastTapTime = tick()

        wait(0.5)
        enabled = true
        clicked = false
    end
end)

--[[--[[Double Jump
enabled = true
Mouse.KeyDown:connect(function(key)
    if not enabled and clicked then
        return
    end
    if key == " " and _G.Energy >= 12.5 then
    enabled = false

    local function onTap()
        if tick() - lastTapTime < 0.2 then
            print("double Click!")
            clicked = true
                --What happends when the user double taps
            player.Character.UpperTorso.Velocity = Vector3.new(0, 100, 0)
            Trail.Parent = Char.HumanoidRootPart
            local attachment0 = Instance.new("Attachment", Char.Head)
            attachment0.Name = "TrailAttachment0"
            local attachment1 = Instance.new("Attachment", Char.HumanoidRootPart)
            attachment1.Name = "TrailAttachment1"
            Trail.Attachment0 = attachment0
            Trail.Attachment1 = attachment1
            wait(0.3)
            Trail:Remove()
            _G.Energy = _G.Energy - 12.5
            wait(1)  
            end
        end

        onTap()
        lastTapTime = tick()

        wait(0.5)
        enabled = true
        clicked = false
    end
end)

--Repleneshing Energy
for i = 1, math.huge do
    wait(0.1)
    print(_G.Energy)
    if _G.Energy < 100 then
        _G.Energy = _G.Energy + 12.5
        print(_G.Energy)
        wait(2)
    end
end

]]--]]
0
I edited my answer hope it helps a little User#21908 42 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I don't completely understand cframes yet so forgive me for not giving a script answer. I think what would work is if you got the lookVector of the player. then teleported them in that direction by setting their CFrame + 50(in the direction the player was looking) leaving all the other parts of the CFrame teleport the same. I hope this helps. Sorry if I am a bit unclear. Have a great day scripting!

Edit 1.

This script prints which way the player is looking:

game.Players.PlayerAdded:Connect(function(plr)
    wait(5)
    while wait(1) do
        print(plr.Character.Head.CFrame.lookVector)
    end
end)

I am guessing you dont want the player to teleport up and down so ignore the y axis when teleporting. I will continue to look for an answer but I hope this helps a little

Edit 2.

Sorry I just dont know enough about CFrames yet to be much help. However there are a couple of helpful resources I can direct you to and some script I tried that you can fiddle with(doesnt work as of now). Here is a link I hope will help you understand the script I wrote and the problem you are facing. Here is the script I tried:

game.Players.PlayerAdded:Connect(function(plr)
    wait(2)

    while wait(2) do

    local distanceToTravel = 50
    local xDirection = plr.Character.Head.CFrame.lookVector.X
    local zDirection = plr.Character.Head.CFrame.lookVector.Z
    local slope = zDirection / xDirection
    local plrY = plr.Character.Torso.CFrame.Y
    local plrX = plr.Character.Torso.CFrame.X
    local plrZ = plr.Character.Torso.CFrame.Z
    local xToGetTo = (plrX + distanceToTravel * 1 / (1 + slope)) or (plrX - distanceToTravel * 1 / (1 + slope))
    local zToGetTo = (plrZ + (slope * distanceToTravel) * 1 / (1 + slope)) or (plrZ - (slope * distanceToTravel) * 1 / (1 + slope))
    plr.Character.Torso.CFrame = CFrame.new(xToGetTo , plrY, zToGetTo)
    end
end)    

there are a couple problems one of which is: what if slope = inf/undefined. Hope this helps you find the answer. If you find the answer please comment that you did and post it in the question so that I can see it. Have a great day scripting, Plegethon5778

0
Thanks for the response! I have this line of code that does what you suggested (At least I hope lol) "player.Character.HumanoidRootPart.CFrame = CFrame.new(cam.CFrame.lookVector + 50, cam.CFrame.lookVector + 50, cam.CFrame.lookVector + 50)" but it doesn't seem to work. Please forgive me if I just wrote it wrong, I'm fairly new to scripting. McQuakyDuck 8 — 5y
0
Let me do some research and get back to you tomorrow evening hope I can help. Good luck until then. Have a great day scripting! User#21908 42 — 5y
0
Aw Thanks a lot! Ill see if I can come up with anything until then :D McQuakyDuck 8 — 5y
0
Sorry I didn't get back to you sooner; my internet cut off at the worst time, and I couldn't fix it for a while. I seemed to have figured it out. It was something abount not being able to add a cframe to a vector3, and someone gave me a line of code that worked. I appreciate all the work that you did to help me, though! McQuakyDuck 8 — 5y
View all comments (2 more)
0
Thank you. Have a great day scripting. User#21908 42 — 5y
0
I was going about the question in the wrong way. Oh well. lol. Til next time, Phlegethon User#21908 42 — 5y
Ad

Answer this question