Chat command for money balance?
Hey guys,
Just wondering if anyone knows how to add a chat command for DarkRP, "/balance" to display the players balance in chat. i.e "Kirk Van Houten's Balance: £9001"
Can't find an addon that adds this
Any ideas?
Cheers
Use getDarkRPVar
And option a) implement a command with darkrps command system or b) use a hook like PlayerSay / OnPlayerChat.
Where would I input this? (just getting to grips with LUA), don't suppose you've got an example code at all?
I found something similar but unsure if it would apply to this, and im not sure which lua file I have to edit.
https://forum.facepunch.com/t/how-to-display-dark-rp-money-with-a-chat-command/230138/3
You can put it anywhere where either the client (for OnPlayerChat) or the server (for PlayerSay) loads it.
hook.Add("OnPlayerChat", "DarkRP_GetBalanceByCommand", function(ply, txt)
if LocalPlayer() != ply then return end
if !string.StartWith(string.lower(txt), "/balance") then return end
LocalPlayer():ChatPrint("Your balance is: "..LocalPlayer():getDarkRPVar("money"))
return true
end)
Ah lovely, i've got that working but it's only showing to the player and not the players around them.
Thanks for your help btw!
Oh, you want to get the balances of other Players. You just need to string.explode and search by the second argument (the playername). If a result is found, use the playerobject instead of localplayer to get the balance.
Nono I meant like if someone is mugging you in DarkRP, they walk up to you with a gun and ask you to input /balance
If you type it in, it displays in the chatbox how much money you are currently holding for others to see, if that makes sense! haha
This is probably a bad way to do it, but use ents.FindInSphere when the command is ran, then print it. If the person is inside the sphere and is a player then show it to them.
something like
or something like that
you could always use an eye trace if you want to get the person's balance who you are looking at.
not even sure how to do this if I'm honest. And I guess it would stop people attempting to mug if they can see their balance before mugging them ;(
I'm pretty close with the code below, but it only displays for the person who inputs the command and not the people around them (like regular chat)
hook.Add("OnPlayerChat", "DarkRP_GetBalanceByCommand", function(ply, txt)
if LocalPlayer() != ply then return end
if !string.StartWith(string.lower(txt), "/balance") then return end
LocalPlayer():ChatPrint("Your balance is: "..LocalPlayer():getDarkRPVar("money"))
return true
end)
If you want something like that you would need to do ents.FindInSphere approach
Also looking at your code above, your running this clientside which is a bad idea if you want to send a message to all players within a radius. You should be doing this serverside with PlayerSay.
This just a case of changing the "OnPlayerChat" part of that code to "PlayerSay" ? I also have it in garrysmod > addons> ulx >lua >ulx modules > sh - not sure if this is right (i'm quite new to LUA)
Cheers again
For what your doing no. This isn't a ULX command, this is just regular gmod lua. You should be placing it in garrysmod/lua/autorun
And there's a few things I would change. I'll give an example as to how I would go about something like this:
There's also no real need to return true. That will just stop any other hooks from running which can prevent some other addon functionality.
Ah right maybe that's where I was going wrong, although I tried adding it as a new script inside the file path suggested and doing /balance still isn't returning anything in game ;(
thanks again for explaining it in layman's
There is a need. It's to prevent the message from showing up in the chat.
Also, your code needs to validate if its a player. And you need to change ply to v :)
How would u write this and where would you put it? I havea somewhat vested interest in this. love to see it working!
Just simply add a if-stamtement that checks for v:IsPlayer() and if it isn't continue (don't return!)
Not sure why I'm struggling with this so much haha
:') thanks for your help guys, I thought this would be an easy fix haha!
Oops. My bad lol. I'll go update it.