Whitelist for chat commands not working

So basically I’m going to have a script that allows whitelisted users in the table to say !host and it will host a tournament where players can join and compete for certain prizes like in-game cash.

So I wanted to start off with a test and just try to use a simple print() function when a whitelisted user says a certain keyword.

My code does not work for some reason.

Code:

local whitelisted = {
	
	"danthespam",
	
	
	
}

game.Players.PlayerAdded:Connect(function(Player)
	
	Player.Chatted:Connect(function(Message)
		if whitelisted[Player.Name]and Message == "test" then
			print("hey")
		end
	end)
end)

The only thing I can see from this code is that you should probably use table.find() instead of whitelisted[Player.Name] as this is an array, not a dictionary/table (whatever it is). Also, you need a space between whitelisted[] and the and.

I’ve written out the new, hopefully working code for you here:

local whitelisted = {
	
	"danthespam",
	
	
	
}

game.Players.PlayerAdded:Connect(function(Player)
	
	Player.Chatted:Connect(function(Message)
		if table.find(whitelisted, Player.Name) and Message == "test" then
			print("hey")
		end
	end)
end)
1 Like

Your script isn’t working because your whitelisted table is an array, meaning that the index still start from 1, whereas if you were to convert this to a dictionary you would be able to set the index to the player’s name and have the value set to a boolean. So in order for your code to work you’d have to do this:

local whitelisted = {
	
	["danthespam"] = true,
	
}
3 Likes

Thank you! Script is working perfectly now.

:grinning:

This code worked too! Thank you.