Creating Exceptions in math.random()

I’m trying to write a script that selects a random KeyCode from Enum.KeyCode using math.random(). I want to exclude W,A,S,D,I,O,T from the list.

cBind=nil

badKeys={
    Enum.KeyCode.W,
    Enum.KeyCode.A,
    Enum.KeyCode.S,
    Enum.KeyCode.D,
    Enum.KeyCode.T,
    Enum.KeyCode.I,
    Enum.KeyCode.O,
}

function ChooseKeybind()
    local keybind=string.char(math.random(string.byte('A'),string.byte('Z'))) 
     cBind=Enum.KeyCode[keybind]
    for _,key in pairs(badKeys) do
        if cBind==key then
            ChooseKeybind()
else
    game.Players.LocalPlayer.PlayerGui.ScreenGui.shootText.Text="PRESS "..tostring(keybind).." TO ESCAPE!"
    end
end
end

However, it’s still selecting KeyCodes from the restricted list. Any help/input is welcome! I’m not the best at scripting so explanations would also be appreciated.

1 Like

Maybe you can try and save it as a string.

local cBind=nil

local badKeys={
	"W",
	"A",
	"S",
	"D",
	"T",
	"I",
	"O",
}

function ChooseKeybind()
	local keybind=string.char(math.random(string.byte('A'),string.byte('Z'))) 
	cBind=Enum.KeyCode[keybind]
	print(cBind)
	for _,key in pairs(badKeys) do
		if cBind==key then
			
			ChooseKeybind()
		else
			print("GoodKey!")
		end
	end
end

while true do
	task.wait(5)
	ChooseKeybind()
end

Just hopped in studio and wrote this couldn’t deal with the formatting on here. But basically what i did was change badKeys to a string value, because u are making keybind a string. then we can reference the badkeys by doing currentBind=Enum.KeyCode[keybind] since Keybind is a STRING, so we just check the table of STRINGS of bad keys. If it matches we re loop through the function. Let me know if this works!

Maybe this instead?

local cBind=nil

local badKeys={
	"W",
	"A",
	"S",
	"D",
	"T",
	"I",
	"O",
}

function ChooseKeybind()
	local keybind=string.char(math.random(string.byte('A'),string.byte('Z'))) 
	cBind=Enum.KeyCode[keybind]
	
	for _,key in pairs(badKeys) do
		if cBind==key then
			
			return ChooseKeybind()
		end
	end
    return cBind
end

while true do
	task.wait(5)
	print(ChooseKeybind())
end

What would be the point in returning cBind if its a local variable throughout the script based off a local script, there wont be a need to return cBind if cBind = Enum.KeyCode[keybind]. cBind can be accessed throughout the script.

local badKeys={
	"W",
	"A",
	"S",
	"D",
	"T",
	"I",
	"O",
}

local function ChooseKey()
	local Key = string.char( math.random( string.byte("A"), string.byte("Z") ) )
	
	if table.find(badKeys, Key) then
		repeat
			Key = string.char( math.random( string.byte("A"), string.byte("Z") ) )
		until not table.find(badKeys, Key)
	end
	
	print(Key)
end

This wont allow blacklisted keys to appear, now do whatever u want with it

Since its array (I think) then you can use table.find to find the blacklisted key inside,
If it does find it, then we repeat random choose until it wont find it

You can also add “return Key” to get what function chose (I forgot to add that)

we can store the key we got and then just run a loop until the key value is not found in our bad keys table.

local badKeys={
	"W",
	"A",
	"S",
	"D",
	"T",
	"I",
	"O",
}

local keycodes = Enum.KeyCode:GetEnumItems()
local function randomKey()
	local key
	repeat
		key = keycodes[math.random(46, 71)] -- 46 to 70 are letters A-Z
	until not table.find(badKeys, key.Name)
	return key
end
1 Like

Good that you’re get it through enum, but it can be confusing a bit at first time (And for someone who don’t know)

(Also how do you even know 46-70 are keyboard keys)
Also its 71
image
image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.