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

Teleport a player around a object?

Asked by 5 years ago

Im trying to teleport a player around a object but i cant get it to work this was my original code (which failed very bad):

function TPPlayer(player, part)
    player.Character.HumanoidRootPart.Anchored = true
    local RandomOriX = math.random(-360, 360)
    local RandomOriY = math.random(-360, 360)
    local RandomOriZ = math.random(-360, 360)
    player.Character.HumanoidRootPart.Orientation = Vector3.new(-90 , -90, -90)
    player.Character.HumanoidRootPart.Position = Vector3.new(part.Position.X, part.Position.Y * part.Size.Y / 2 - 5, part.Position.Z)
end

I then tryed this code but it just doesnt do it right: (this is what happens)

function TPPlayer(player, part)
    player.Character.HumanoidRootPart.Anchored = true
    local playerpos = player.Character.HumanoidRootPart.Position
    local partpos = part.Position
    player.Character.HumanoidRootPart.CFrame = CFrame.new(partpos,playerpos) * CFrame.new(0,0,5)
end

The way i want it is so the player is randomly teleported around the object, so one tome you could be on top of the ball / next you could be underneath it or on left right etc.

A very bad illustration

i will take any help given!

0
LookVector, RightVector, and UpVector. DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
5 years ago
Edited 5 years ago

I know a pretty easy method to update the position of something based on the relative position of another thing.

If you do:

partA.CFrame = CFrame.new(partB.CFrame * Vector3.new())

it takes the part you wanna change (partA, or in your case, the character) and gives it a new position based on the origin of partB (the ball).

So I rigged up a little test script for you to mess around with. You can adjust the values however you like, but its much simpler than the CFrame math you were doing.

--//StarterCharacterScripts

local userInputService = game:GetService("UserInputService")

local character = script.Parent --parent would be the character
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local part = workspace:WaitForChild("Part") --add a part into workspace, in your case, the ball

userInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then 

        --random values, edit these how you may like. 
        local randomX = math.random(-5, 5)
        local randomY = math.random(-5, 5)
        local randomZ = math.random(-5, 5)

        humanoidRootPart.CFrame = CFrame.new(part.CFrame * Vector3.new(randomX, randomY, randomZ))
    end
end)

Let me know if you need any help. https://gyazo.com/db930049e0abb994c7ec934ab67cb42a

Check out this gif of what it should look like.

0
Oh and i literally just noticed you said, "The way i want it is so the player is randomly teleported around the object, so one tome you could be on top of the ball / next you could be underneath it or on left right etc." Sigh. Maybe youll still learn something from this hopefully. You can essentially use this code and just add some if-statements to do it. Psudar 882 — 5y
1
works just fine! just added a line to make player face the object and it works perfect! thank you so much NubScripters 126 — 5y
0
Np dude Psudar 882 — 5y
Ad

Answer this question