Difficulties getting the player [Solved]

What I’m trying to do should be fairly simple, but for some reason my code has been sadistically failing, and not achieving my academic goals that I am trying to reach.

local PS = game:GetService('Players')

local Msg= '/example kyler_josiah' 
local Name=string.sub(msg,9,msg:len())--works(gets name)

local Plr = PS.Name-- doesn't work

And although I have tried many solutions, most ineffectually failed. Concluding that the best contender would be online.

is this on the server or on the client

Do you want all the players? And is this a local or server script

1 Like

Could you give some more information? I don’t really understand what you are trying to achieve. Is the ‘/example kyler_joshiah’ eventually gonna be used in a Chatted event?

1 Like

Yes, the code would be executed through the Chatted function.

A selected player. Server sided.

You can use Player.Chatted, I suggest looking through the link I put.
Here is an example though

game.Players.PlayerAdded:Connect(function(player) -- Player is the player that was added

  player.Chatted:Connect(function(msg) -- msg is the message sent
        -- do stuff with msg and player
        local Plr = player.Name -- Will work
  end)

end)

Sadly i do not have any expierence with string.sub etc. Haven’t really learned it. But is this something ur looking for?

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == '/example kyler_josiah' then
			print(plr.Name)
		end
	end)
end)

I believe this is what you’re trying to accomplish, but I do believe you’re better off using string.split to parse messages.

local PS = game:GetService('Players')

local Msg= '/example kyler_josiah' 
local Name=string.sub(Msg,10,Msg:len())

local Plr = PS[Name]

Btw, You are not using the

local PS = game:GetService('Players')

Anywhere in your code. :wink:

I see what you’re looking for

local players = game:GetService("Players")
local message = "/example uwuCulturist"

local filteredMessage = string.split(message," ")
local playerName = filteredMessage[2]
local player = players:FindFirstChild(playerName)
if not player then
    return warn(("Player '%s' not found"):format(playerName))
end
print(player)

On top of this, you’re trying to index nil instead of using findfirstchild so that’s your second error

K there I edited it and removed game.Players

This exactly meets the standard. And I originally did use “FindFirstChild”.