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

How to get a part change randomly? (Read For More Detail)

Asked by 8 years ago

OK, so I'm trying to script a part that randomly becomes invisible and CanCollide, and visible + Cannot Collide. I written this script, but it doesn't seem to work, maybe because I left out some information. here is what I have:

local wall = script.Parent
local m = math.random (1,(2))
enabled = true

if m == 1 then
    wall.Transparency = 1
    wall.CanCollide = false
end

if m == 2 then
    wall.Transparency = 0
    wall.CanCollide = true
end

I placed this script inside the part.

I don't really know, but am I missing something important so it can function properly?

1 answer

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

I believe you did your math.random incorrectly.

It should be as followed:

local m = math.random(2) -- this chooses a random number ranging from 1 to 2. It won't pick 0.

Here is your script redone by me to function as intended.

local wall = script.Parent
number = 2 -- Change this number to how long you want it to take for the loop to recheck.
enabled = true

while wait(number) do --added a while true do loop so it repeats every 2 seconds
    m = math.random(2) -- moved it here so it chooses a random number (1 or 2) every time the loop restarts. (set as 2 seconds)

    if m == 1 then
        print(1)
        wall.Transparency = 1
        wall.CanCollide = false
    end

    if m == 2 then
        print(2)
        wall.Transparency = 0
        wall.CanCollide = true
    end
end
Ad

Answer this question