Script Not Working... Again

I’m making a computer system for a game I’m making and this script isn’t working, can someone help?

-- Main commands
local maincommands = {
	store = "store"
}

-- Subcommands
local subcommands = {
	deny = "deny",
	confirm = "confirm"
}

-- Checks if its a valid command
function isValidCommand(input, commandList)
	for _, command in ipairs(commandList) do
		if input == command then
			return true
		end
	end
	return false
end

-- This is the function that handles everything
local function onEnterPressed()
	local userInput = script.Parent.Text:lower():gsub("^%s*(.-)%s*$", "%1") -- Trim spaces and convert to lowercase

	-- Check if the input is a valid main command
	if isValidCommand(userInput, maincommands.store) then
		print("Valid main command: Store")
		script.Parent.Parent.ScrollingFrame.TextLabel.Text = "Welcome to the store! Items are listed below!\n*Placeholder $5"
	end

	-- Check if the input is a valid sub command
	if isValidCommand(userInput, subcommands.confirm) then
		print("Valid sub command: Confirm")
	end
end

-- Using this to check the command when enter is pressed
script.Parent.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		onEnterPressed()
	end
end)

Please elaborate. Without further information we can’t really help

1 Like
  1. Your isValidCommand function expects a table of commands to loop through, but strings are being passed to it (e.g. maincommands.store).

  2. Use pairs instead of ipairs within the isValidCommand function as ipairs are meant for arrays but you’re storing commands as key-value pairs.

if isValidCommand(userInput, maincommands) then
	if maincommands[userInput] == "store" then
		print("Valid main command: Store")
		script.Parent.Parent.ScrollingFrame.TextLabel.Text = "Welcome to the store! Items are listed below!\n*Placeholder $5"
	end
end

A more direct approach for your use case could be:

if maincommands[userInput] then
    	if maincommands[userInput] == "store" then
        	print("Valid main command: Store")
		script.Parent.Parent.ScrollingFrame.TextLabel.Text = "Welcome to the store! Items are listed below!\n*Placeholder $5"
	end
end
2 Likes

The

onEnterPressed()

function isn’t setting the text nor printing

1 Like

Sadly the script didn’t work.
But I could see it working!
Even though it didn’t work, thank you!

1 Like