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*
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
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!