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

it wont teleport for some reason. I'm using math.random and im not that good at it?

Asked by 4 years ago
game.Players.PlayerAdded:Connect(function(plr)
    while true do
        wait(1)
    local character = plr.Character
        character.HumanoidRootPart.Position = CFrame.new(math.random(118.701, 2.9, 61.094), math.random(60.81, 2.9, 94.995))
    end
end)
0
Hey I see you're having trouble, ill explain it for you. :D B_rnz 171 — 4y

2 answers

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago
Edited 4 years ago

Math.random() requires two values: min, and max. It returns an integer from min to max, unless Math.random() is not given any arguments. When not given an argument, it returns a value between 0 and 1. This means that it can return something bizare like 0.512333333333.

However, if you want to get an irrational number from math.random(), you can multiply the base function by a constant (like math.random()*100), or divide math.random() with arguments by a constant (like math.random(1, 5000)/100).

If you want to give a random position, you should use math.random three times for the CFrame, like this:

character.HumanoidRootPart.CFrame = CFrame.new(math.random(0, 118), 2.9, math.random(60, 94)) or something along those lines depending on what you wish. Enjoy!

p.s. position weld/constraint-bound objects via CFrame otherwise their welds would be deleted and they'll fall out of the world. :(
p.p.s position should be cframe. cframe is like cursive position, it models 3D space and rotation. it also maintains the welds and constraints when used.
0
If you want to maintain the player's rotation, you should call CFrame:ToEulerAnglesXYZ(), like so: HumanoidRootPart.CFrame = CFrame.new() * CFrame.Angles(HumanoidRootPart.CFrame:toEulerAnglesXYZ()) Fifkee 2017 — 4y
Ad
Log in to vote
0
Answered by
B_rnz 171
4 years ago

So the first error that I see, you're using CFrame, and that you should use Vector3 like this:

character.HumanoidRootPart.Position = Vector3.new()

Also, Vector 3 should have 3 parameters, for example:

character.HumanoidRootPart.Position = Vector3.new(0, 0, 0) -- 0,0,0 are parameters

But you're trying to make it go random right? So thats when math.random come in! Make sure your math.random has 2 parameters and not 3. Something like:

character.HumanoidRootPart.Position = Vector3.new(math.random(1, 60), math.random(1, 60), math.random(1, 60))

You can edit this anyway possible. And thats all! Hope this works and helps you out! :D

0
You should NEVER use Vector3 on character descendants. They will break any joints related and will end up falling through the world. Always position Humanoid parts via CFrame. Fifkee 2017 — 4y
0
Actaully, if it's bound by a weld or constraint (Motor6Ds included), then just use CFrame. When in doubt, you should probably use CFrame. Fifkee 2017 — 4y
0
Just because it doesn't error doesn't necessarily mean it will work the way you intend it to. Fifkee 2017 — 4y
0
But he was calling Position, and not CFrame, thats why I used Vector. B_rnz 171 — 4y
0
..I never even noticed that. Oops Fifkee 2017 — 4y

Answer this question