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

How does one make a randomly Collide/NoCollide Brick?

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*

1 comment

0

Remember that you can press that "Lua" button in order to create code blocks.

2 answers

✓ Accepted answer

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

1 comment

0

It worked! Thank you, i'll try and use random seed from now on.

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!

1 comment

0

This worked aswell, thank you!

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