How do I find the player that fits a description?

I am making a votekick system and need help getting the player that is supposed to be votekicked.

-- Client Side
-- the client side works fine
player.Chatted:Connect(function(Text)
if string.sub(Text, 1, 10) == ";votekick " or string.sub(Text, 1, 10) == ";Votekick " or string.sub(Text, 1, 10) == ";VOTEKICK "then
local Suspect = string.sub(Text, 11)
local Charamount = string.len(Suspect)
game.ReplicatedStorage.VoteKick:FireServer(Suspect, Charamount)
end
end)
-- Server Side
game.ReplicatedStorage.VoteKick.OnServerEvent:Connect(function(Victim, Suspect, Datafile)
for i,Witnesses in pairs(game.Players:GetChildren()) do

				if Witnesses.Name:sub(1, Datafile) == Suspect then
                                 -- here is where I need help
                                 -- I want to know how I can find the player that the (Suspect) is


				-- local done = (Witnesses.Name:sub(1, Datafile) == Suspect)
                                  -- I tried doing this local but it didn't seem to work

-- Any solutions???
				game.ReplicatedStorage.VoteKick:FireAllClients()			
     else
          print("no wintesses")
          end
     end
end)

The client-side can easily be transferred over to the server by doing:

local command = ";votekick "

function GetPlayer(name)
  for _, player in pairs(game.Players:GetPlayers()) do
    if player.Name:lower() == name then
      -- "Name" has to be the exact player name
      return player
    end
  end
end

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(Text)
    if string.sub(Text, 1, 10):lower() == command then --doesnt matter if its ";VoTeKick" will still register because the string is lowered to all common letters using string.lower(string) or str:lower()
     local Suspect = string.sub(Text, 11)
     local SuspectPlayer = GetPlayer(Suspect)
     print(SuspectPlayer)
     --SuspectPlayer would be the player that was called out to be votekicked, fire to all clients or do whatever you'd like
    end
  end)
end)

No need for remote events.

P.S -
I’m not sure if this is gonna work, if it errors or doesn’t work, please come back.
There’s another way to do it but I didn’t want to confuse you.

2 Likes

Hello there, Slayec! First of all, your second line is useless. Instead, you could simply change it to if string.sub(Text, 1, 10):lower() == ";votekick ".

Second thing, I personally wrote a vote kick system which is perfectly working. Here’s the script:


-- Personally, I'm never using ``string.sub``, so I'm using a table instead.

function GetName(PartialName) -- This function will return the full name of a player.
	local PlayersTable = {}
	for i, v in pairs(game.Players:GetChildren()) do
		PlayersTable[i] = v.Name
	end
	local Length = string.len(PartialName)
	local Sub = string.sub(PartialName, 1 ,Length)
	local Target = nil
	for index = 1, #PlayersTable do
		local PlayerFound = string.sub(PlayersTable[index], 1, Length)
		if Sub == PlayerFound then
			Target = PlayersTable[index]
		end
	end
	return Target
end

-- Example: warn(GetName(Hyp))
--> HyperD3v

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		
		local Arguments = {}
		
		for Argument in string.gmatch(Message,"[^%s]+") do
			table.insert(Arguments, Argument)
		end
		
		if Arguments[1]:lower() == ";votekick" then -- If the message starts by ``;votekick``
			if Arguments[2] ~= nil then -- If a second argument is given
				local Suspect = GetName(Arguments[2])
				if Suspect then -- If the player is found in the server
					game.ReplicatedStorage.VoteKick:FireServer(Suspect)
				else
					warn("No player were found!") -- No player found.
				end
			end
		end
	end)
end)

DEVFORUM1
DEVFORUM2

I hope this may help you!

1 Like