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

How does one make a randomly Collide/NoCollide Brick?

Asked by
Sy_vin 7
3 years ago

Basically, i've been trying to make a repeating function where a brick either can collide, or can't collide. Every 3-4 seconds, it repeats the script. So in theory the script should randomly choose if its going to be a nocollide or collide part, and then in a set amount of time, it chooses again. I tried making it myself, but I have no idea what i'm doing, and I need massive help.

Heres my attempt: * local num = math.random(1,2) while true do if num == 1 then script.Parent.CanCollide = false wait(0.4) elseif num == 2 then script.Parent.CanCollide = true wait(0.4) end end*

0
Remember that you can press that "Lua" button in order to create code blocks. ResDXsention 108 — 3y

2 answers

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

Try:


-- random seed -- if omitted, math.random will keep getting the same number sequence no matter how many times it is run math.randomseed(os.time()) -- make sure to loop while true do local n = math.random(1, 2) if n == 1 then part.CanCollide = true else part.CanCollide = false end wait(4) -- or whatever end
0
It worked! Thank you, i'll try and use random seed from now on. Sy_vin 7 — 3y
Ad
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

What you are doing is randomising the number only once. This means the same number is used every time. In this case, we are going to use math.randomseed() to find your local time and set the random number depending on your time.

math.randomseed(tick()) -- the seed for math.random is changed depending on your local time!
while true do 
    local num = math.random(1,2) -- Always different!
        if num == 1 then 
            script.Parent.CanCollide = false 
            wait(3) 
            else -- Don't need to write elseif as there are only two possibilities!
            script.Parent.CanCollide = true 
            wait(4) 
        end 
end

Hopefully, this works! Please mark it as an answer if it does!

0
This worked aswell, thank you! Sy_vin 7 — 3y

Answer this question