I am attempting to make a script so that when the player presses ‘E’ it opens their inventory, unless they are looking at a merchant, in that case it will open the merchant’s store. The way I am doing this is that open the player presses a key (I am using .InputEnded) it will call a module script, sending the key the player pressed and an id (variable selection) specifying whether the player is looking at a merchant. My code looks like this:
function controls.getInteraction(key, selection)
local returnVal = nil
local interactions = {
[Enum.KeyCode.E] = {"inventory", "store"},
-- I may add some more keys here later
}
if (table.find(interactions, key, 1)) then
returnVal = interactions[key][selection]
end
return returnVal
end
My problem was that if the player pressed a key like ‘W’, it would throw an error because it doesn’t exist in the table, so I then added a check using table.find() to first make sure the key existed. Now my problem is that any key put into this function returns nil. What am I doing wrong? Is there a better way of doing this? Thanks.