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

Teleporting players to one of the positions given randomly?

Asked by
4xN 10
8 years ago

So, I've been trying to make a script so that players would be teleported to one of the locations given randomly. I'm not to familiar with math.random or anything random in general. Here is something I tried below. If I can figure out how to work this, I'm also going to want to have more locations to be teleported to randomly.

function teleport()
script.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(math.random(-194.983, 101.68, 1.904, -235.392, 69.082, 41.8))
end
script.Parent.MouseButton1Down:connect(teleport)

2 answers

Log in to vote
0
Answered by 8 years ago

You're close! Basically what math.random() does it it Returns a random value between the parameters set. So if I write something like,

print(math.random(1,5))

Then the output could be any of these, 1 2 3 4 5 One of the problems with math.random() is it only works with whole numbers. Meaning, if I do something like this,

print(math.random(0.874,2.01))

Then the output could NOT be something like 1.67, because it's not a whole number. Moving on to your problem. You tried using more than two parameters for your math.random. You can only give two! But you want three random numbers so you would Need to use three math.random()s. Like this,

function teleport()
    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(math.random(-195, 101),math.random( 2, -235),math.random( 69, 42))--I changed all of your script.Parents to local player for simplicity.
end
script.Parent.MouseButton1Down:connect(teleport)

And that should work. Good Luck!

0
I understand math.random now. Thanks! 4xN 10 — 8y
0
<3 User#11440 120 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Also if you'd like "math.random()" to give you decimals, you can simply do this:

x = math.random(0,9) + math.random(0,10)/10

This script will give you numbers with decimals from 0 to 10.

0
Basic math but you're right. Maybe I shouldn't assume people understand division. User#11440 120 — 8y
0
But for the record, this will not give a random decimal "from" 0 to 10. The lowest number that x could equal is 0.1 and the highest is 10. Just a little error in case he doesn't understand division. User#11440 120 — 8y
0
Edited, sorry, it was a detail. Dolphinous 36 — 8y
0
Lol, sorry for snapping User#11440 120 — 8y

Answer this question