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 6 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!

01--Moving the Humanoid
02    --Double Tap
03enabled = true
04Mouse.KeyDown:connect(function(key)
05    if not enabled and clicked then
06        return
07    end
08    if key == "w" and _G.Energy >= 25 then
09    enabled = false
10 
11    local function onTap()
12        if tick() - lastTapTime < 0.2 then
13            print("double Click!")
14            clicked = true
15                --What happends when the user double taps
View all 89 lines...
0
I edited my answer hope it helps a little User#21908 42 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 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:

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

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:

01game.Players.PlayerAdded:Connect(function(plr)
02    wait(2)
03 
04    while wait(2) do
05 
06    local distanceToTravel = 50
07    local xDirection = plr.Character.Head.CFrame.lookVector.X
08    local zDirection = plr.Character.Head.CFrame.lookVector.Z
09    local slope = zDirection / xDirection
10    local plrY = plr.Character.Torso.CFrame.Y
11    local plrX = plr.Character.Torso.CFrame.X
12    local plrZ = plr.Character.Torso.CFrame.Z
13    local xToGetTo = (plrX + distanceToTravel * 1 / (1 + slope)) or (plrX - distanceToTravel * 1 / (1 + slope))
14    local zToGetTo = (plrZ + (slope * distanceToTravel) * 1 / (1 + slope)) or (plrZ - (slope * distanceToTravel) * 1 / (1 + slope))
15    plr.Character.Torso.CFrame = CFrame.new(xToGetTo , plrY, zToGetTo)
16    end
17end)   

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 — 6y
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 — 6y
0
Aw Thanks a lot! Ill see if I can come up with anything until then :D McQuakyDuck 8 — 6y
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 — 6y
View all comments (2 more)
0
Thank you. Have a great day scripting. User#21908 42 — 6y
0
I was going about the question in the wrong way. Oh well. lol. Til next time, Phlegethon User#21908 42 — 6y
Ad

Answer this question