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

click detector and math random?

i tried to make when player click on brick then math.random then brick changing color, but it not works

script.Parent.MouseClick:Connect(function()

local number = math.random(1, 2)  
number = math.random(1, 2)
print(number)
	if math == 1 then	
	print("1")	
script.Parent.Parent.BrickColor = BrickColor.Random()
    if math == 2 then	
script.Parent.Parent.BrickColor = BrickColor.Random()
    print("2")
end
end
		end)

2 answers

Maybe switching out the if math to if number will work + i added "elseif" so if the number isnt 1 it will do number 2

script.Parent.MouseClick:Connect(function()

local number = math.random(1, 2)  
number = math.random(1, 2)
print(number)
    if number == 1 then   
    print("1")  
script.Parent.Parent.BrickColor = BrickColor.Random()
    elseif number == 2 then   
script.Parent.Parent.BrickColor = BrickColor.Random()
    print("2")
end
end
        end)

If this doesn't work try to keep the "elseif" and switch back to "if math" instead of "if number"

Hopefully this works/helps!

2 comments

0

No need to define number twice

0

There's no need for number at all if you're just going to use BrickColor.Random() on the same part. It doesnt make it extra random XD

You can delete that math.random. Because the number was 1 or 2 same to random brick color.

script.Parent.MouseClick:Connect(function() -- fire the MouseClick Event

function OnClicked() -- when clicked
	script.Parent.Parent.BrickColor = BrickColor.Random() -- Random brickColor
	print(math.random(1, 2) -- print 1 or 2
end) -- end (if you end a function then you need to use 'end)' )
       
script.Parent.MouseClick:Connect(function(OnClicked) -- when clicked fire the OnClicked function

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