Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I need help finding a player easier- without using i,v in pairs?

Asked by 3 years ago
Edited 3 years ago

I tried to use for i,v in pairs, but my code keeps looping through the players, and every time it does this, it repeats "error"- until it gets to a matching player. I know this is supposed to happen; but is there an easier way?

Commands.warn = function(Sender,Arguments)
    local Username = Arguments[1]
    table.remove(Arguments, 1)
    local Message = table.concat(Arguments, " ")
    if Username == string.lower(Sender.Name) then
        fWarn(Sender,"Error: You can't warn yourself.",true,"rbxassetid://26399276")
    else
    for i,v in pairs(game.Players:GetChildren()) do
            if string.lower(v.Name) == Username then
            fWarn(Sender,"Sucessfully Warned "..v.Name.."!",true,"rbxassetid://377245168")
            fWarn(v,"You have been warned by"..Sender.Name..": "..Message,true,"rbxassetid://377245168")
        else
            fWarn(Sender,"Error.",true,"rbxassetid://26399276")
        end
    end
    end
end

Thanks in advance.

For some context: I'm trying to make it so that when an admin says !warn {Player} {Reason}, that function will fire. I already have the commands working, I just need a better way of finding the player, one that does not involve looping through them all.

0
can u accept my answer? Xyternal 247 — 3y

1 answer

Log in to vote
0
Answered by
Xyternal 247 Moderation Voter
3 years ago
Edited 3 years ago

So here is the code. In between the ifs, you add your code. https://scriptinghelpers.org/questions/70527/how-to-stringfind-a-specific-player

local Players = game:GetService("Players")

local function onPlayerAdded(player)
    print("Player: " .. player.Name)
    if player.Name = "TheName" then
    --your code here
    end
end


for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
0
not sure what you mean TheVerySussyImposter 7 — 3y
0
check the link out Xyternal 247 — 3y
Ad

Answer this question