So I was making admin commands (non module so far) and I was thinking of having autofilling names. Here is my code I have tried a lot I don’t know how to use string.match and if you can help heres my code:
commands.unfreeze = function(sender,targetPlr1, targetPlr2,command)
if table.find(admins,sender.UserId) then
if game.Players:FindFirstChild(targetPlr1) then
game.Players:FindFirstChild(targetPlr1).Character.HumanoidRootPart.Anchored = false
elseif table.find(specialAdmins,sender.UserId) then
if game.Players:FindFirstChild(targetPlr1) then
game.Players:FindFirstChild(targetPlr1).Character.HumanoidRootPart.Anchored = false
end
end
end
end
yea I gave you the unfreeze command
and if you need it, here is my chat thingy:
game.Players.PlayerAdded:Connect(function(plr)
if table.find(serverBans,plr.UserId) then
plr:Kick("You're banned, "..plr.Name)
end
plr.Chatted:Connect(function(msg)
local splittedMessage = msg:split(" ")
local MessageWithPrefix = splittedMessage[1]:split(prefix)
local saidCommand = MessageWithPrefix[2]
local targetPlayer1 = splittedMessage[2]
local targetPlayer2 = splittedMessage[3]
if commands[saidCommand] then
commands[saidCommand](plr,targetPlayer1,targetPlayer2,saidCommand)
end
end)
end)
if this is the wrong topic, let me know I can move it.
I would avoid having to loop through every single player every time the player types/removes a character. Especially in large servers this can cause a short (barely noticable) delay.
Instead, I would create a map of all the players and the first characters of their names beforehand:
local Players = game:GetService("Players")
local map = {}
local maxSearchCharacters = 8
local function addPlayer(player)
for x = 1, maxSearchCharacters do
if x >= #player.Name then break end
if map[v.Name:sub(1, x)] then
table.insert(map[v.Name:sub(1, x)], v)
else
map[v.Name:sub(1, x)] = { player }
end
end
end
Players.PlayerAdded:Connect(addPlayer)
This would only have to be done once every time a player is added and is stored so it can be used later. Here’s an example of how the above map could be used:
--// NOTE: This event isn't real, I'm just explaining how to do it
oncharactertyped(function(newFullMessage)
if map[newFullMessage] then
--[[
atleast 1 player with a name that starts with
what was entered in the text label
]]
for _, player in ipairs(map[newFullMessage]) do
-- a player with a matching name
end
end
end)
Responding on what I said, u could use local Players = game.Players:GetChildren()
then whenever u are going to see/get the players who are in the server u could call Players.
Here you go this should work! You can call the function whenever you want and just put the shortened name when calling the function like so:
function FindPlayer(name) -- name = string provided to look for player
for i,v in pairs(game.Players:GetPlayers()) do -- iterating though players
if v.Name:lower():sub(1,#name) == name:lower() then -- checking to see if player name matches the provided string
return v -- returns the player it finds
end
end
end
FindPlayer('top')