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

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

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)"

1 comment

0

Because the target wasn't found.

3 answers

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)

2 comments

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.

0

Still didn't work.

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

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)

1 comment

0

Nothing worked. :/ I got no error but it didn't work

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