Getting string to PLAYER:

So i have a DLISTVIEW thing that displays all players on the server. Now with the click of a button i want to be able to kick the player selected. This is example of the menu i have now http://i.imgur.com/J03VtqP.png

and i can get the string of the player selected when a button is clicked using



print ( PlayerList:GetLine( PlayerList:GetSelectedLine() ):GetValue(1) )


How do i turn the string into PLAYER: so i can do



ply:Kill()


or



ply:Kick()


and etc

Thank you

I mean how to turn string into player. as if “some_lame_person”:GetID() or something

You need the player object. I assume that you’re getting the player names from the player object, so, on button click, you can just use that player object:Kick(). But you will need to send it to server so the server kicks the player, as clients cant. http://wiki.garrysmod.com/page/Net_Library_Usage for that.

So, you are probably doing like:
[lua]
DLabel:SetText( ply:Nick() );
[/lua]

So you could just do it like:
[lua]
DLabel:SetText( ply:Nick() );
DButton.OnClick = function()
net.Start( “KickPly” );
net.WriteFloat( ply:EntIndex() );
net.SendToServer();
end;
[/lua]

and serverside:
[lua]
net.Receive( “KickPly”, function( len, calling_ply )
local ply = Entity( net.ReadFloat() );
ply:Kick();
end );
[/lua]

You need to check if ply:IsValid(), check that the calling ply is allowed to do it etc.
And you are not using DLabel for your list, but I think that you understand.

Something along the line of this should work…

Serverside
[lua]
util.AddNetworkString(“slayply”)

function FindPlayer(ply) --Function to find the player, using the nick passed.
if !ply then return false end
for k,v in pairs(player.GetAll()) do
if string.lower(v:Nick()) == string.lower(ply) then
return v
end
end
return false
end

net.Receive(“slayply”, function(len, client)
if !client:IsAdmin() then return end
local plnick = net.ReadString()
local plobj = FindPlayer(plnick) --Find the player!
plobj:Kill()
end)
[/lua]

Clientside
[lua]
button.DoClick = function() --Assuming the button to slay the player is named button; else change it.
net.Start(“slayply”)
local nick = PlayerList:GetLine( PlayerList:GetSelectedLine() ):GetValue(1)
net.WriteString(nick)
net.SendToServer()
end
[/lua]

Is there a reason for using string.lower?

With my little Lua experience, I cannot say for sure, but what is the differnece between:

if v:Nick == ply

and

if string.lower( v:Nick ) == string.lower( ply )

if the string is “lOwer” and the player’s name is “LOWER” the string won’t catch his name. using string.lower will make the string “lower” and the player’s name “lower”, making it so it will catch him.

Lua is case sensitive.

diarrhea is not equal to Diarrhea

This is why a lot of the time you will have to lower both of the strings to see if they match.

Why in the world would you use that string as an example? It’s Christmas…

Hey! BLAHHHHHHHHHHHHHHHHHH I found that quite BLAHHHHHHHHHHH offensive! BLAHHHHHHHHHHHHHHHHHHHHHHHHH