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

How can I make a random function?

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

It's supposed to be:

if number == 4 then
  part:Destroy()
end
0
oh! I always wondered why some scripts had 2 equal signs. Thanks! Borkingforlife 9 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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

Answer this question