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

Trying to get Player target from message:sub() but dosen't work?

Asked by
RootEntry 111
5 years ago

Hi, I am trying to make it so if you for example say "/explode iiLevelMaker" it will explode the player they mentioned.

I tried using message:sub, but it didn't work or I didn't use it the right way.

Here is the script:

repeat wait() until game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Players = game.Players
local Keys = game:GetService("ReplicatedStorage").Keys
local Doors = workspace.RoomDoors
local Char = Player.Character
local GroupID = 4394811
local MinRank = 3
local AllRooms = {"Room 1","Room 2","Room 3","Room 4","Room 5","Room 6","Room 7","Room 8","Room 9","Room 10","Room 11","Room 12","Room 13","Room 14","Room 15","Room 16","Room 17","Room 18","Room 19","Room 20","Room 21","Room 22","Room 23","Room 24","Room 25","Room 26","Room 27","Room 28","Room 29","Room 30"}

Player.Chatted:Connect(function(message)
    if Player:GetRankInGroup(GroupID) >= MinRank then
        for i,v in pairs(AllRooms) do -- Get All Rooms
            local GetTargetFromArgument = message:sub(2) -- Get Player object from argument
            local Target = Players:FindFirstChild(GetTargetFromArgument) -- Find that player that was mentioned in the chat argument.

            print(Target.Name) -- Printing the target's name.
        end
    end
end)

Error: "16:05:18.765 - Players.iiLevelMaker.PlayerGui.DoorSystem_CLIENT:16: attempt to index local 'Target' (a nil value)"

0
Because the target wasn't found. User#19524 175 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I have no idea where message:sub came from (fairly sure it's non-existent too), but have you tried doing this instead?

local GetTargetFromArgument = string.sub(message, 2)

0
string.sub(message) or message:sub() both work, message is a string and in the eyes of lua there is nothing wrong with it. jaschutte 324 — 5y
0
Still didn't work. RootEntry 111 — 5y
Ad
Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
5 years ago

The player might have typed the name incorrectly, if that happens target will be nil.

local GetTargetFromArgument = message:sub(2)
local Target = Players:FindFirstChild(GetTargetFromArgument)
if target then --checks if the target has been found
    print(Target.Name) --print the target out succesfully.
else --target hasn't been found, now giving out a custom error.
error("Target hasn't been found. "..Player.Name.." has tried to target "..GetTargetFromArgument.." . Which didn't exist.")
end
Log in to vote
0
Answered by
tantec 305 Moderation Voter
5 years ago
Edited 5 years ago

What you can do is run through all the players rather than trying to use findfirstchild, this is what I've done

repeat wait() until game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Players = game.Players
local Keys = game:GetService("ReplicatedStorage").Keys
local Doors = workspace.RoomDoors
local Char = Player.Character
local GroupID = 4394811
local MinRank = 3
local AllRooms = {"Room 1","Room 2","Room 3","Room 4","Room 5","Room 6","Room 7","Room 8","Room 9","Room 10","Room 11","Room 12","Room 13","Room 14","Room 15","Room 16","Room 17","Room 18","Room 19","Room 20","Room 21","Room 22","Room 23","Room 24","Room 25","Room 26","Room 27","Room 28","Room 29","Room 30"}

Player.Chatted:Connect(function(message)
    if Player:GetRankInGroup(GroupID) >= MinRank then
    local Target = nil - our variable for now
        for i,v in pairs(AllRooms) do -- Get All Room
        for _, plr in pairs(game.Players:GetPlayers()) do
            local plrname = plr.Name:sub(1, message:len()):lower()
            if plrname == message:lower() then
                Target = plr
            end
        end
        if target then
             print(Target.Name) -- Printing the target's name.
            end
        end
    end
end)
0
Nothing worked. :/ I got no error but it didn't work RootEntry 111 — 5y

Answer this question