Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I use a table to put an input.KeyCode statement?

##The title question might not make sense but i'll try my best to further explain it.

So i'm trying to make a script where theres are 2 tables of keys to press {E,G,Y,C,L} and {ButtonA,ButtonB,RB,LB,DPadDown} (and yes, i know RB/LB arent actual Enum things but theyre just for the example, ill change them later to actually fit.) to either counter a players attack or attack a player but i'm trying to use this same tables of keys so I don't have to write an if statement with 9 'and's. Is this possible?

Heres the script (by the way, i'm not actually focusing on any errors in the script yet, I just need to make a rough copy so I can look back and fix it later... but of course if you do wanna point something out i'll be glad to listen.)

p=game.Players
player=p.LocalPlayer
uis=game:GetService("UserInputService")
reps=game:GetService("ReplicatedStorage")
event=reps.CombatSystemEvent
mouse = player:GetMouse()
fight=false
ready=false
fightable=true
allowedtocounter=false
enemyplayer=nil
keys1={"E","G","Y","C","L"}
keys2={"ButtonA","R2","L1","ButtonB","DPadDown"}

uis.InputBegan:Connect(function(input,gp)
	if uis:GetFocusedTextBox() then return end
	if input.KeyCode==Enum.KeyCode.F or input.KeyCode==Enum.KeyCode.Thumbstick1 then
		print("attemptng to fight something.") 
		if not fight and fightable then
			print("im not fighting anyone right now, continue to advance.") 
			local selected = mouse.Target
			if selected.Parent.ClassName == "Model" and p:GetPlayerFromCharacter(selected.Parent) then
				enemyplayer=p:GetPlayerFromCharacter(selected.Parent) print("found someone to fight with... "..enemyplayer) 
			else
				print("advance cancelled, nothing to fight with.")
				return
			end
			local diceroll = math.random(1,4) print("rolling some dice.") 
			event:FireServer(enemyplayer, "Start", diceroll) print("launching event to let me fight.")  --nothing actually happens here anyways, its just for later like i said.
			fight=true
		end
	end
	if fight then
		-- this is the statement im trying to use keys1 and keys2 but i dont know if its possible do this this.
	end
end)

1 answer

✓ Accepted answer

You could use table.find to see if the key is in your table. Also .Name is a property of an Enum

if table.find(keys1, Input.KeyCode.Name) then
	-- key is in keys1 table
end

1 comment

0

thx

This discussion is preserved as it was. New answers, comments, and votes are closed.