I have a script below for a random number generator that generates a random number inside a variable. The player then selects the number they think it is by stepping onto the correct part (Currently there is only one part). The script then tells them if they are right or wrong.
The player should then step off the part and a new number should be generated and they would try to select the new number they think it is.
However, currently when a player steps on a part, the output is seen multiple times and new numbers are generated whenever a player steps on a different part of the brick. I think I need to use debounce to fix this but I can't seem to get it to work. The script in full is below.
Help with this would be appreciated.
math.randomseed(tick())
math.randomseed( os.time() )
math.random() math.random() math.random()
local debounce = false
function GenerateNewNumber()
GenerateNumber = math.random(1,7) -- The number the computer has generated. I.e. what block is the correct block within the workspace.
SelectedNumber = 0 -- The number the player has selected I.e. what block they stepped on.
if GenerateNumber == 1 then
print("1")
elseif GenerateNumber == 2 then
print("2")
elseif GenerateNumber == 3 then
print("3")
elseif GenerateNumber == 4 then
print("4")
elseif GenerateNumber == 5 then
print("5")
elseif GenerateNumber == 6 then
print("6")
elseif GenerateNumber == 7 then
print("7")
else
error("Invalid Number")
end
end
GenerateNewNumber()
script.Parent.Touched:connect(function(player)
if debounce == false then
debounce = true
SelectedNumber = 1
if SelectedNumber == GenerateNumber then
print("Yes, You guessed the correct number!")
else
print("Sorry, but the correct number was " .. GenerateNumber)
end
wait(1)
GenerateNewNumber()
debounce=false
end
end)

1 comment
Could you just do a loop to check after the first touch if the player is still touching and if they are just set debounce to true?