Pivity

Chat command for money balance?

Last read Never
#1

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

#2

Use getDarkRPVar

And option a) implement a command with darkrps command system or b) use a hook like PlayerSay / OnPlayerChat.

#3

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

#4

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)

#5

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!

#6

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.

#7

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

#8

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

[balance command ran here]
for k, v in pairs(ents.FindInSphere(player origin, radius) )
if !player then return end
v:ChatPrint(name.."has"..money)

or something like that

Disagree x 1
#9

you could always use an eye trace if you want to get the person's balance who you are looking at.

Agree x 2
#10

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)

#11

If you want something like that you would need to do ents.FindInSphere approach Gordon said earlier (unless DarkRP has a function to do this i don't know about).

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.

Agree x 1
#12

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

#13

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:

-- Create the hook.
hook.Add("PlayerSay", "ohmygoodnessgraciousisthatlivacodoingsomethingforonce", function(ply, text)
-- Check if it's our command or not. If not, stop.
if(!string.StartWith(text, "/balance")) then return end

-- Get the balance and store it in a var to prevent multiple getDarkRPVar calls.
local bal = ply:getDarkRPVar("money")
-- Loop through every entity within x radius. Change this value to what you want.
for k,v in ipairs(ents.FindInSphere(ply:GetPos(), 500)) do
-- If they aren't a player then nono.
if(!v:IsPlayer()) then continue end

-- Send a message with the player's balance.
v:ChatPrint(ply:Name() .. " has a balance of $" .. bal .. ".")
end
end)

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.

Agree x 1
#14

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

#15

There is a need. It's to prevent the message from showing up in the chat.

#16

Also, your code needs to validate if its a player. And you need to change ply to v :)

#17

How would u write this and where would you put it? I havea somewhat vested interest in this. love to see it working!


#18

Just simply add a if-stamtement that checks for v:IsPlayer() and if it isn't continue (don't return!)

#19

Not sure why I'm struggling with this so much haha

mark272 Your suggestion below works but only seems to display to the player who puts the command in and

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)

Livaco Your code works but displays only to the player who inputs it and prints it around 75 times

-- Create the hook.
hook.Add("PlayerSay", "ohmygoodnessgraciousisthatlivacodoingsomethingforonce", function(ply, text)
-- Check if it's our command or not. If not, stop.
if(!string.StartWith(text, "/balance")) then return end

-- Get the balance and store it in a hook to prevent multiple getDarkRPVar calls.
local bal = ply:getDarkRPVar("money")
-- Loop through every entity within x radius. Change this value to what you want.
for k,v in ipairs(ents.FindInSphere(ply:GetPos(), 500)) do
-- Send a message with the player's balance.
ply:ChatPrint(ply:Name() .. " has a balance of $" .. bal .. ".")
end
end)



:') thanks for your help guys, I thought this would be an easy fix haha!

#20

Oops. My bad lol. I'll go update it.

Users viewing this thread
Nobody