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

I hate doing this, but could someone please help me?

Asked by 10 years ago

I keep getting this error:

Workspace.Script:3: attempt to index local 'plr' (a nil value)

When making my admin script. I have a script set up in the workspace like this:

game
    workspace
        script

I have tried many different type of ways to do this. This is my script:

~~~~~~~~~~~~~~~~~ game.Players.PlayerAdded:connect(function(plr) plr.Chatted:connect(function(msg) if msg:sub(1,6) == "/kill " then game.Players:FindFirstChild(msg:sub(6)):BreakJoints(); end end) end) ~~~~~~~~~~~~~~~~~

But I get no errors, and nothing happens.

2 answers

Log in to vote
0
Answered by 10 years ago

Your problem may be line 4; You did not code that line correctly; You did line 3 correctly, but not line 4, this is what is happening;

-Line 3 --Check if 'plr' typed '/kill ' at the beginning of the command
-Line 4 --Check in Players for a Child with the name ' PlayerName'

As you look carefully, there is a Space, when you call '/kill PlayerName', it is also adding the ' ' to the string because you did not code the sub correctly [String Manipulation], considering upping the sub number.

Now, let me explain to you the sub method;

local String = "I'm a String!" --Here is our string

print(String:sub(1,5)) --As you'll see in the Output, it has printed 'I'm a', that is because it got the string between the 'Start' to the 'Pos' [or Position]
print(String:sub(5)) --And now as you'll see, in the Output, it has printed 'a String!', that is because it skipped to the fifth character in the string; It went to the Pos of where to start the 'sub'

Now, all we need to do now is fix your script, let's re-code it a bit to make it look a bit more clean; We are going to create a new function, and redo your command, for, your other will not work;

function PlrAdded(plr) --Here is our new function, as you'll see, 'plr' is an Identifier for when the Player joins the server/game
plr.Chatted:connect(function(msg) --This will fire each time the Player has chatted

if msg:lower():sub(1,6) == "/kill " then --This will check to see if what the Player chatted started with '/kill '
local Plr = game.Players:FindFirstChild(msg:sub(7)) --This will check to see if the Player is existant, will return nil if not
if Plr and Plr.Character then --This will check to see if the Player, and the Player's Character is existant
Plr.Character:BreakJoints() --This will kill the Player; The 'BreakJoints' method breaks the Joints of 'BasePart' type instances in a 'Model' type instance
end --This ends the code block for the first 'if' statement
end --This ends the code block for the second 'if' statement

end) --This ends the code block for the '.Chatted' event/function
end --This ends the code block for the function

game.Players.PlayerAdded:connect(PlrAdded) --Now, whenever a 'Player' joins, it will fire the 'PlrAdded' function, and fire the event 'PlayerAdded' [The 'PlayerAdded' event will not work in Studio mode]
for i,v in pairs(game.Players:GetPlayers()) do PlrAdded(v) end --This will loop through all the current Players in 'Players', and fire the 'PlrAdded' event for each Player

Hope this helped, and sorry if I did not give a good explanation!

Ad
Log in to vote
-2
Answered by 10 years ago
game.Players.PlayerAdded:connect(function(plr)
    plr.Chatted:connect(function(msg)
        if msg:sub(1,6) == "/kill " then
            game.Players:FindFirstChild(msg:sub(6)):BreakJoints();
        end
    end)
end)
0
It's msg:sub(7) to get the player you're looking for. Character 6 is a space, so you'd be trying to get a player with a space at the beginning of the name. Tkdriverx 514 — 10y
0
Don't just post code, explain your answer in English. Perci1 4988 — 10y
0
@Perci, This is the exact same code as asked in the question, He obviously dosent know how the edit button works, so he posted it here c; Uroxus 350 — 10y
0
Not the point.. The point is that it doesn't do ANYTHING! MatutinaStella 0 — 10y

Answer this question