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

Can someone define for me how the script selects the target player?

So I'm gonna script a gui with a textbox, what I plan on doing is having it kill the target in the box, I'm new to this type of scripting, I've been looking at this script from the wiki trying to figure out what each part means, what I'm asking is if someone can define what it does when finding the target player, etc. Thank you for taking the time to read or help. I'm not asking for a script to do what I'm trying to do btw, just asking to help define. (Lines 2-18) I have a basic understanding of scripting.

local isAdmin = {["DontBlinkDrWho"] = true, ["Admin1"] = true, ["Admin2"] = true}
 
function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end
 
function onChatted(message, player)
    if message:sub(1, 5) == "kill/" and isAdmin[player.Name] then
        victim = findPlayer(message:sub(6))
        if victim and victim.Character then
            victim.Character:BreakJoints()
        end
    end
end
 
 
game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

1 comment

0

so you're trying to make an admin gui button where you type a name and it kills the person?

1 answer

✓ Accepted answer

Heyo, so:

When the onChatted() function is triggered by somebody typing into the game chat, it triggers code that checks the first five letters of the message for "kill/" ("message:sub(1, 5)"), to confirm it is a kill command being requested. It then takes the rest of the message ("message:sub(6)") and calls the findPlayer() function, with whatever you typed in after "kill/" assigned to the variable "name".

findPlayer() then runs a generic for loop, running through all the players in the server. Let's look through that code.

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

Ok, the first line defines the block of code as a function, so that it can return data and be run multiple times with ease.

The second line begins the loop, with "player" as the variable name for the player object, not the player's name.

The third line checks if the player's name in lower case matches the lower case version of the name in the textbox. This allows you to ignore capital letters in a name.

If it finds a player with a matching name, it returns that player object (once again not the player name), and then ends the loop. If the names do not match however, it runs the loop again with the next player on the server.

When it returns the player to the onChatted() function, the function checks that the character is spawned in, and, if they are, breaks its joints, removing the torso from the head and instantly killing the player.

This is my first post on this site, so if you have any questions please ask.

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