Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use math.random

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

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.

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)

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.