Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to find the nearest name of a player?

I was wondering how I would find the name of closest players name? Lets say I want to go ahead and say something like kill void or something. It would run and kill me. So far Ive been using this code which doesnt find the nearest target

   player.Chatted:Connect(function(message) -- trying to make and admin script
    
    if checkifadmin(player) then
    
    if string.lower(string.sub(message,1,string.len("kill")))== "kill" then
    
    print("Working")
    
    local target = game.Players:FindFirstChild((string.lower(string.sub(message,6))))
    
    local targetname = target.Name
    
    local charecter = game.Workspace:WaitForChild(targetname)
    
    print(charecter)
    
    charecter.Humanoid.Health = 0
    
    end
    
    end
    
    end)

1 answer

✓ Accepted answer

What you are looking for is string.find() first argument is the string you are checking, the second one is the string you are using to check the string from arguemnt 1. The rest are optional. It can be found on this link -> https://developer.roblox.com/api-reference/lua-docs/string . The way string.find() works is that it will return the location of where the small section of the string is. For example:

local var1 = string.find("potato","ota") 
print(var1) -- prints "2,4" 
--[[ Because in "2" letters 
that's where it starts and in "4" 
letters, that is where it ends.]]

5 comments

0

I can explain more because i made admin once

0

Could you explain how I use string.find?

0

She literally just explained it for you. How is this not clear enough?

0

No what to input in it i don't understand the output and input of string.find?

0

note string.find will return nil if it does not find anything of the sub string

This discussion is preserved as it was. New answers, comments, and votes are closed.