I'm back from yesterday hoping you kind people can once again help me with my script. I am very novice at lua so I appreciate any help or a step in the right direction. But I am here to learn!
I'm currently modifying DarkRP so it has a stats derma window which shows playtime and such. I'm attempting to make it so that you have a chat command which opens the menu for you, or if you specify a player. The code works but not when I attempt it with derma panels.
I'm unsure on how to make my derma panels show other players information when specified.
From my sv module:
[lua]
local function Stats(ply, args)
local name = string.sub(args, 1)
target = DarkRP.findPlayer(name)
if name == "" then
ply:ConCommand("statsopen")
return ""
end
if target then
ply:ConCommand("statsopen")
else
DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("could_not_find", tostring(name)))
end
return ""
end
DarkRP.defineChatCommand("stat", Stats)
[/lua]
and a snippet from my client file
[lua]
local NameLabel = vgui.Create("DLabel", StatsMenu)
NameLabel:SetPos(300,30) // Position
NameLabel:SetColor(Color(255,255,255,255)) // Color
NameLabel:SetFont("ChatFont")
NameLabel:SetText(LocalPlayer():Nick(),"TargetID") // Text
NameLabel:SizeToContents() // make the control the same size as the text.
[/lua]
Obviously I need to change the LocalPlayer():Nick() to something else, but I'm unsure what I need to define, nor change it to.
Once again, and help or a step in the right direction is appreciated. Thank you for reading.
I suggest for this purpose to use usermessages instead of the console command.
Use the usermessages to send the entity object of the player you specified, which in this case seems to be [B]target[/B]. So once you receive the entity client-side, use that instead of LocalPlayer.
EDIT: Fixed some grammatical problems, typed that up pretty fast.
Also you may use a console command to trigger the usermessage since I'm assuming you still want the function triggered by a command.
So I need to somehow define the string as the target then?
Use an argument for your command, have it search all the players for that piece of the name using [B]string.find[/B] on the player's name. You could even use [B]table.getn([/B] [I]tablename[/I] [B])[/B] to get the amount of arguments the player inputted into the console command, loop through the amount and build the string to search for.
You could make it even more accurate by searching for a STEAM_ID but I believe the colons in the steam ID count as another argument so there'd be a bit more work. The players [B]ent_index[/B] could work too.
Anyways, once you find the specified player entity, start a user message with the recipient being yourself. Include the target player in the user message and receive it via client-side.
Hook the user message with this:
[url]http://wiki.garrysmod.com/page/usermessage/Hook[/url]
snip
Alright, I looked into it a bit more and came up with this:
ServerSide
[lua]
local function Stats2(ply, args)
local name = string.sub(args, 1)
target = DarkRP.findPlayer(name)
if name == "" then
print("self")
return ""
end
for _, v in pairs( player.GetAll() ) do
if target then
umsg.Start("StatsPlayer")
umsg.String(string.find(v:Nick(), name))
umsg.End()
print("else")
else
DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("could_not_find", tostring(name)))
end
return ""
end
end
DarkRP.defineChatCommand("stat", Stats2)
[/lua]
ClientSide:
[lua]
function StatsMenu()
local StatsMenu = vgui.Create("DFrame")
StatsMenu:SetSize(500, 500)
StatsMenu:Center()
StatsMenu:SetTitle( "Stats" )
StatsMenu:ShowCloseButton( true )
StatsMenu:SetDraggable( false )
StatsMenu.Paint = function()
surface.SetDrawColor( 0, 128, 200, 230 )
surface.DrawRect( 0, 0, 500, 25)
surface.SetDrawColor( 81, 81, 81, 150 )
surface.DrawRect( 0, 0, StatsMenu:GetWide(), StatsMenu:GetTall() )
surface.SetDrawColor( 50, 50, 50, 50 )
surface.DrawOutlinedRect( 0, 0, StatsMenu:GetWide(), StatsMenu:GetTall() )
end
local NameLabel = vgui.Create("DLabel", StatsMenu)
NameLabel:SetPos(300,30)
NameLabel:SetColor(Color(255,255,255,255))
NameLabel:SetFont("ChatFont")
NameLabel:SetText(name.Nick(),"TargetID")
NameLabel:SizeToContents()
StatsMenu:SetVisible( true )
end
usermessage.Hook( "StatsPlayer", StatsMenu );
concommand.Add("statsopen", StatsMenu)
[/lua]
But it's having trouble indexing 'name'.
What should I change this to?
Bump
[QUOTE=Handsome Matt;42721642]nobody will help you if you don't post full errors, it's a lot more work for us to read through your entire script and try to guess where the error is.[/QUOTE]
[lua]
[ERROR] addons/darkrpmodification/lua/darkrp_modules/statsmenu/cl_statsmenu.lua:38: attempt to index global 'name' (a nil value)
1. Function - addons/darkrpmodification/lua/darkrp_modules/statsmenu/cl_statsmenu.lua:38
2. unknown - lua/includes/modules/usermessage.lua:87
[/lua]
Names not getting defined clientside. Somethings not sending over right.
[QUOTE=Handsome Matt;42731668][lua]NameLabel:SetText(name.Nick(),"TargetID") [/lua]
name isn't defined at all. since you're hooking StatsMenu to a usermessage you need to define it as:
[lua]function StatsMenu(um)[/lua]
then you can do this instead:
[lua]NameLabel:SetText(um:ReadString(),"TargetID")[/lua][/QUOTE]
Hmm... that's only drawing back "1" instead of the targets name.
Looks like that's working good, Thanks!
Although, How can I use that to get the players health as well as the name?
Can I have multiple things sent from umsg.Short and read in different areas?
(Health, Armor... etc)
-snip-
Thanks a lot Matt and Jeezy.
Got everything working for the most part...
Sorry, you need to Log In to post a reply to this thread.