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