How to Fix Attemp to call table value

hii
image
this is the code i wrote
resim_2024-05-05_203617283
and this is the table
image
how could i fix this bug

You can check if an element is in an array with table.find().

if table.find(wasd, input.KeyCode) then

input is a keyword… 1st rule of naming any identifier is that it shouldn’t be a keyword

soln to your issue:

if table.find(wasd, Input.KeyCode) then
   ---
end

That’s just the forum’s syntax highlighting. input is not a keyword in Lua nor Luau to my knowledge.

1 Like

You don’t even need a table for this:

local c = input.KeyCode.Name
if c == "W" or c == "A" or c == "S" or c == "D" then
	--your code
end

I would still recommend using a table because if more inputs were added, using a giant chain of or statements would become problematic

Using or statements is useful when the statements aren’t a lot, and in this case, they aren’t. If we assume a scenario where the statements are a lot(lets say more than 10) using a table is the way to go.

1 Like

I find it completely useless as people use table.find here as I’m seeing.

Simply do this;

if wasd[input.KeyCode] then
-- your code

And that’s it!

The table is an array, not a dictionary so that wouldn’t work

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