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

How can I make a random function?

I am working on a game and I am trying to make a part where it randomly breaks. Here is my script:

local number = math.random(1,5)
local part = script.Parent

if number = 4 then
	part:Destroy()
end

Help would be appreciated!

3 answers

✓ Accepted answer

Ya made the mistake of not putting 2 equals in.. Oops..

It's supposed to be:

if number == 4 then
  part:Destroy()
end

1 comment

0

oh! I always wondered why some scripts had 2 equal signs. Thanks!

Whenever you use "if" and an equals sign, there must be two equal signs.

In a simpler way:

if number == 4 then
  part:Destroy()
end

Even though there was 2 answers, I wanna state that one equal means for "set". Two equals means for "equals to". So using one equals sign in an if statement, it does not work and always return false. It is because if statement never get pass the requirement of setting the number to 4 in the if statement and it never will.

Also, if statement only runs once, means that if the number randomize and its 1, 2, 3 or 5, it won't repeat for you and it stops. You need to make a loop and cover them all up.

Script:

local RandomNumber = math.random(1,5)
local Part = script.Parent

if RandomNumber == 4 then
	Part:Destroy
	else
	-- If the number is not 4, then do what code. You can delete the "else" if you do not want this.
end

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