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):
1 | function TPPlayer(player, part) |
2 | player.Character.HumanoidRootPart.Anchored = true |
3 | local RandomOriX = math.random(- 360 , 360 ) |
4 | local RandomOriY = math.random(- 360 , 360 ) |
5 | local RandomOriZ = math.random(- 360 , 360 ) |
6 | player.Character.HumanoidRootPart.Orientation = Vector 3. new(- 90 , - 90 , - 90 ) |
7 | player.Character.HumanoidRootPart.Position = Vector 3. new(part.Position.X, part.Position.Y * part.Size.Y / 2 - 5 , part.Position.Z) |
8 | end |
I then tryed this code but it just doesnt do it right: (this is what happens)
1 | function TPPlayer(player, part) |
2 | player.Character.HumanoidRootPart.Anchored = true |
3 | local playerpos = player.Character.HumanoidRootPart.Position |
4 | local partpos = part.Position |
5 | player.Character.HumanoidRootPart.CFrame = CFrame.new(partpos,playerpos) * CFrame.new( 0 , 0 , 5 ) |
6 | 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.
i will take any help given!
I know a pretty easy method to update the position of something based on the relative position of another thing.
If you do:
1 | partA.CFrame = CFrame.new(partB.CFrame * Vector 3. 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.
01 | --//StarterCharacterScripts |
02 |
03 | local userInputService = game:GetService( "UserInputService" ) |
04 |
05 | local character = script.Parent --parent would be the character |
06 | local humanoidRootPart = character:WaitForChild( "HumanoidRootPart" ) |
07 | local part = workspace:WaitForChild( "Part" ) --add a part into workspace, in your case, the ball |
08 |
09 | userInputService.InputBegan:Connect( function (input) |
10 | if input.KeyCode = = Enum.KeyCode.F then |
11 |
12 | --random values, edit these how you may like. |
13 | local randomX = math.random(- 5 , 5 ) |
14 | local randomY = math.random(- 5 , 5 ) |
15 | local randomZ = math.random(- 5 , 5 ) |
16 |
17 | humanoidRootPart.CFrame = CFrame.new(part.CFrame * Vector 3. new(randomX, randomY, randomZ)) |
18 | end |
19 | end ) |
Let me know if you need any help. https://gyazo.com/db930049e0abb994c7ec934ab67cb42a
Check out this gif of what it should look like.