I created my custom chat commands using AlvinBlox’s tutorial. This script looks fine to me, though it isn’t working. I changed things a bit and didn’t fully script everything how he scripted it, but as I already said, it looks fine to me. The script isn’t giving any errors either… My script:
--Variables--
local admintable = {969915904}
local commands = {}
local playersSV = game:GetService("Players")
function findplayerbyusername(username)
for i, player in ipairs(playersSV:GetPlayers()) do
if string.lower(player.Name) == username then
return player
end
end
return nil
end
--Script--
commands.kick = function(sender, arguments)
local playertokickname = arguments[1]
local reason = arguments[2]
if playertokickname then
local playertokick = findplayerbyusername(playertokickname)
if playertokick then
if reason then
playertokick:Kick([[
Een moderator heeft je uit het spel gezet.
Reden: ]] .. reason)
else
playertokick:Kick([[
Een moderator heeft je uit het spel gezet.]])
end
end
end
end
playersSV.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if table.find(admintable, player.UserId) then
local splittedmessage = string.split(string.lower(message), " ")
local command = string.split(splittedmessage[1], "!")[2]
if table.find(commands, command) then
local arguments = {}
for i = 2, #splittedmessage, 1 do
table.insert(arguments, splittedmessage[i])
end
commands[command](player, arguments)
end
end
end)
end)
You’re using table.find on a dictionary. I believe table.find only checks number indices so to fix the script you would just directly look up the command in the dictionary like so:
Oh wait, I understand now. table.find() doesn’t work because the content isn’t the command name, it’s the function. The index is the command name so I should use:
table.find only iterates the number indices, e.g: arr[1], arr[2], arr[3] ... arr[#arr] and not strings or anything else.
Another example:
-- Array with number keys
local Array = {
[1] = "Hello",
[2] = "world",
[3] = "!"
}
-- Dictionary with string keys
local Dictionary = {
key1 = "Hello",
key2 = "world",
key3 = "!"
}
print(table.find(Array, "world"))
-- prints "2" because it found "world" at index 2
print(table.find(Dictionary, "world"))
-- prints "nil" because there is no "world" value at any of the number indices ([1], [2], etc.), in fact, there are no number indices at all since it's just a dictionary.
I believe the table.find algorithm is just something like this:
local function find(arr, value)
for i = 1, #arr do
if arr[i] == value then
return i
end
end
end
There is no need to use table.find, since you already know the index of the value you want to have. You should generally try to avoid table.find as it is slower and more complex.