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

The random button press script doesn't work and I see no isuess any help?

I have made a random generator to make a red or green block appear but it won't show the block or always is Red and it's not changing no matter how many time's I click it. Here is my **Script **

local RN = math.random (1, 2)
local GB = script.Parent.GreenButton
local RB = script.Parent.RedButton

GB.ProximityPrompt.Triggered:Connect(function()
	if RN == 2 then
		script.Parent.RedBlock.Transparency = 0
		GB.ProximityPrompt.Archivable = false 
		wait(5)
		script.Parent.RedBlock.Transparency = 1
		GB.ProximityPrompt.Archivable = true
	end
		
end)


RB.ProximityPrompt.Triggered:Connect(function()
	if RN == 2 then
		script.Parent.RedBlock.Transparency = 0
		GB.ProximityPrompt.Archivable = false 
		wait(5)
		script.Parent.RedBlock.Transparency = 1
		GB.ProximityPrompt.Archivable = true

	

	end
end)

GB.ProximityPrompt.Triggered:Connect(function()
	if RN == 1 then
		script.Parent.RedBlock.Transparency = 0
		GB.ProximityPrompt.Archivable = false 
		wait(5)
		script.Parent.RedBlock.Transparency = 1
		GB.ProximityPrompt.Archivable = true
	end

end)

RB.ProximityPrompt.Triggered:Connect(function()
	if RN == 2 then
		script.Parent.RedBlock.Transparency = 0
		GB.ProximityPrompt.Archivable = false 
		wait(5)
		script.Parent.RedBlock.Transparency = 1
		GB.ProximityPrompt.Archivable = true
	end
end)

1 answer

The red block appears because you only reference the red block in your code, not the green block.

I also would combine the seperate if statements into a shorter if elseif to make your code shorter, and make the RN re-define every time so it's actually randomised.

local RN = 0
local GB = script.Parent.GreenButton
local RB = script.Parent.RedButton

GB.ProximityPrompt.Triggered:Connect(function()
	RN = math.random(1, 2)
	if RN == 2 then
		script.Parent.GreenBlock.Transparency = 0
		GB.ProximityPrompt.Archivable = false 
		wait(5)
		script.Parent.GreenBlock.Transparency = 1
		GB.ProximityPrompt.Archivable = true
	end

end)

RB.ProximityPrompt.Triggered:Connect(function()
	RN = math.random(1, 2)
	if RN == 1 then
		script.Parent.RedBlock.Transparency = 0
		GB.ProximityPrompt.Archivable = false 
		wait(5)
		script.Parent.RedBlock.Transparency = 1
		GB.ProximityPrompt.Archivable = true
	end

end)

Hope this helps.

1 comment

0

now it's just picking the color I pick I wan't it to be random

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