I need help with a "/profile" chat command but I don't understand what is wrong with the code

opensteampage = true

local function OpenSteamProfile(ply, text, team)
player.GetAll()

if (opensteampage == Player:IsConnected() ) then

Player:ShowProfile()

else
opensteampage = false
player:ChatPrint(“Player has either not connected yet or joined the server!”)
end

if text == “/profile … player.Nick()” then
opensteampage = true
end
end

hook.Add(“PlayerSay”, “OpenSteamProfile”, OpenSteamProfile)

There’s quite a lot of problems, so I tried to add comments to each of the issues:



opensteampage = true -- This should be local


local function OpenSteamProfile(ply, text, team)
player.GetAll() -- This returns a table of players. You can't just put it there. You have to do something with it, for example, loop through it.

if (opensteampage == Player:IsConnected() ) then -- 'Player' does not exist.

Player:ShowProfile() -- 'Player' does not exist.

else
opensteampage = false
player:ChatPrint("Player has either not connected yet or joined the server!") -- 'player' does not exist.
end

if text == "/profile .. player.Nick()" then -- This will always be true as it is just a string. It doesn't do what you expect it to because you didn't join it with the variable properly.
opensteampage = true
end
end

hook.Add("PlayerSay", "OpenSteamProfile", OpenSteamProfile) -- This won't work because PlayerSay is serverside and ShowProfile is clientside


It seems that you don’t have much idea of how Lua works to be honest.
It might be worth looking at examples or doing more research.

This should do what you were trying to do, but as you can see it’s completely different:



local function OpenSteamProfile( ply, text )

	if ply ~= LocalPlayer() then return end -- OnPlayerChat is called for every single player that chats, so only show the profile if you requested it.

	if string.sub( text, 1, 8 ) == "/profile" then -- If the first 8 characters of what the player said are '/profile'

		local name = string.sub( text, 10 ) -- The name will be anything past 10 characters

		local ply -- This makes a nil variable to be set as whatever player is found below:
		for _, v in ipairs( player.GetAll() ) do -- For each player in all the players, find the one with the right name
			if v:Nick() == name then ply = v end -- If the name is equal to the player's nickname then set it
		end

		if ply then ply:ShowProfile() end -- If it found a player then show their profile

		return true -- Stops the message from displaying in the chat

	end

end

hook.Add( "OnPlayerChat", "OpenSteamProfile", OpenSteamProfile )


I tried to explain how it works with some comments, but it probably still makes no sense.
The only real help I can give is to say that you need to research into the way Lua works.
It’s the only way any of this will make sense.

thank you for helping me, but I was wondering why you used “ipairs” instead of “pairs” and what is the difference in glua?

ipairs is meant to be faster for tables that are sequential, for example, tables that have indexes 1, 2, 3, 4, 5, 6 and so on.

player.GetAll() is sequential like this, so I usually use ipairs just in case it’s a little bit faster.

There’s a more official but confusing explanation here.

ipairs goes through in sequential order numerically starting from 1, while pairs has no order.

Damn ninjas…

awesome thank you so much and I realise I am not the greatest at glua, one of my problems is “connecting” everything together, thanks for your help!

[editline]29th September 2017[/editline]

ohh sick thanks for the explanation