How to update the Return value

So I am kind of new using “Returns”, I created a table with some Keyboard hotkeys and a function to randomly pick 1 letter, every time I click a click detector it’s randomly picks a letter and works well but the return won’t return the updated letter ,only the first letter the game picked on start.

P.S I am very sorry for my English grammar

here’s the code :

local Buttons = {"K","G","H","J","T","Y","U","I"}

function RandomPick()
	local rand = Random.new()
	local num = rand:NextInteger(1,#Buttons)
	local choosenKey = Buttons[num]
	
	print(choosenKey)
	
	return choosenKey
	
end

local TheKey = RandomPick()

game.Workspace.Part.ClickDetector.MouseClick:Connect(function()
	RandomPick()
	print(TheKey)
end)

You’ll have to do: TheKey = RandomPick() in the MouseClick event to update the variable

1 Like

The problem you are having is that you are outputting the variable which you assigned outside of the event. All you have to do is assign TheKey variable to the new value. Here is the solution to this problem:

local Buttons = {"K","G","H","J","T","Y","U","I"}

function RandomPick()
	local rand = Random.new()
	local num = rand:NextInteger(1,#Buttons)
	local choosenKey = Buttons[num]
	
	print(choosenKey)
	
	return choosenKey
end

game.Workspace.Part.ClickDetector.MouseClick:Connect(function()
	local TheKey = RandomPick()
	print(TheKey)
end)

Hopefully, this helps you <3

1 Like