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

How to use math.random

Asked by 10 years ago

How do i use the (math.random) in teleporting stuff and things like brick.color

function onTouch(part)
    local t = part.Parent:FindFirstChild("Torso")
    if t then
        t.BrickColor = BrickColor.new(math.random)
        t.CFrame = CFrame.new(math.random)(math.random)(math.random)
    end
end 

script.Parent.Touched:connect(onTouch)

i know that (math.random) just goes from 0 to 1.

3 answers

Log in to vote
1
Answered by
User#2 0
10 years ago

You have a few errors here.

First is your BrickColor. BrickColors don't take numbers in the way your trying to use it. If you want a random brickcolor use BrickColor.Random()

Then your CFrame. CFrame takes 3 arguments. Arguments are separated by commas. So you'd do CFrame.new(x, y, z).

And finally, you're trying to get random numbers. I don't have a good way to explain this, so here's a wiki article on random numbers. It explains math.random and such.

Ad
Log in to vote
1
Answered by 10 years ago

math.random(MinimumValue, MaximumValue)

Here is an example:

x = math.random(1, 10)
print(x)

The above code will randomly choose a number between 1 - 10 (integer) and print it, if you don't put anything between the parenthesis it will choose a random number between 0 - 1 (will be pointless if you want to teleport randomly).

Like 18cwatford said, CFrame takes 3 arguments that are seperated by commas CFrame(x, y, z), the way you are trying to use it is wrong, correct way: part.CFrame = CFrame.new(x, y, z) So if we want to teleport to random locations we would use: part.CFrame = CFrame.new(math.random(minX, maxX), math.random(minY, maxY), math.random(minZ, maxZ))

Use this syntax to select a random brick color: part.BrickColor = BrickColor.new(math.random(1,255)/255, math.random(1,255)/255, math.random(1,255)/255) The first argument in the BrickColor is red, the second is green and third is blue.

So this is how your code is suppose to look like:

function onTouch(part)
    local t = part.Parent:findFirstChild("Torso")
    if t ~= nil then
        t.BrickColor = BrickColor.new(math.random(1,255)/255, math.random(1,255)/255, math.random(1, 255)/255)
        t.CFrame = CFrame.new(math.random(minX, maxX), math.random(minY, maxY), math.random(minZ, maxZ))
    end
end

script.Parent.Touched:connect(onTouch)
Log in to vote
0
Answered by
blowup999 659 Moderation Voter
10 years ago

To get a random number you just do math.random(smallest random number you want, biggest random number you want) so in your script you would do

function onTouch(part)
local t = part.Parent:FindFirstChild("Torso")
if t then
t.BrickColor = BrickColor:random()
t.CFrame = CFrame.new(math.random(x,x),math.random(y,y),math.random(z,z))
end
end
script.Parent.Touched:connect(onTouch)

Answer this question