Jump to content

pay someone some money


Mathias Lui

Recommended Posts

Hey world. :D I wanted to make a script which gives a desired player a desired amount of money. like /pay player money..  but when I try this with myself, like /pay myself money, it will just give me money, and not subtract it first. So i have 100$, I do /pay myself 100, and then I have 200$ xD So I don't know any further. Here's my full code with comments...

[lua]

function pay(playerSource, cmd, player, amount)
local playermoney = getPlayerMoney(playerSource) --Don't know why I wrote this but when I deactivate this line, it makes no difference
local x, y, z = getElementPosition(playerSource) --get Position of the paying Player
local x1, y1, z1 = getElementPosition(getPlayerFromName(player)) --get Position of player who will recieve the money
local dist = getDistanceBetweenPoints3D(x, y, z, x1, y1, z1) --gets distance between the two players
local playermoney = getPlayerMoney(playerSource) --gets Money of the paying Player
local targetmoney = getPlayerMoney(getPlayerFromName(player)) --gets Money of the player who will recieve the money
local amount = tonumber(amount) --makes the amount typed in to a number. Amount is the money amount
if not player or not amount then --if player or amount wasnt typed in there is a warning message
    outputChatBox("Usage: /pay Name Amount", playerSource, 255, 0, 0)
else --if everything is typed in correctly...
if (playermoney >= amount) then --if you have at least as much money as you wanna pay
    if (dist <= 10) then --and you are maximum 10 meters away from the recieving player
        setPlayerMoney(playerSource, playermoney-amount) --here's the problem. This should take the desired amount of money away
        setTimer(function() end, 1000, 1) --and this should be a pause, but it does nothing. btw Is there an easy way to make a pause of 1 second?
        setPlayerMoney(getPlayerFromName(tostring(player)), targetmoney+amount) --gives the recieving player the desired amount of money
        outputChatBox(getPlayerName(playerSource).." gave you "..amount.." $!", getPlayerFromName(player), 0, 255, 0) --message to inform both players
        outputChatBox("You gave "..player.." "..amount.." $!", playerSource, 0, 255, 0) --also just a message
    else --if the player is not in range to the other player there is a warning message (below)
        outputChatBox("The desired player is not in range.", playerSource, 255, 0, 0) --just warning message
    end --ends the distance if-statement

else --if you haven't got at least as much money as you wanna pay there is a warning message, which also works (below)
    outputChatBox("You don't have enough money.", playerSource, 255, 0, 0) --just warning message
end --ends playermoney if-statement
end --ends typing correctly if-statement
end --ends the whole pay function
addCommandHandler("pay", pay) --command. Usage /pay target money

[/lua]

 

The meta isn't the problem. Just a server sided script.

 

hope you can help me!

Link to comment

The problem with all this is that you're not using the live amount but a stored amount, it works when you send money to a different player but when you're sending to yourself you have your first amount of money stored in both targetmoney and playermoney so when you're giving money to yourself you basically ignore the money you took and subtracts from the first value. I'd suggest you use givePlayerMoney() and takePlayerMoney() instead of get/setPlayerMoney(), that way you'd specify an amount to give or take instead of having to calculate how much money you have before each transfer and then do the math yourself.

Here's an example of a similar function using give/takePlayerMoney instead:

--[[ Commands to transfer money between players ]]--
function sendMoneyToPlayer(player, cmd, receiver, amount)
	local money = tonumber(amount) or 0
	if receiver and money and money > 0 and getPlayerMoney(player) >= money and getPlayerTeam(player) then
		local playerReceiver = getPlayerFromName(receiver)
		if playerReceiver then
			takePlayerMoney(player, money)
			givePlayerMoney(playerReceiver, money)
			exports.GTWtopbar:dm(money.."$ sent to "..receiver, player, 0, 255, 0)
			exports.GTWtopbar:dm(money.."$ received from: "..getPlayerName(player), playerReceiver, 0, 255, 0)
			outputServerLog("[BANK] $"..money.." sent to: "..receiver..", from: "..getPlayerName(player).." ("..getTeamName(getPlayerTeam(player))..")")
		else
			exports.GTWtopbar:dm("Player does not exist", player, 255, 0, 0)
		end
	elseif money < 0 then
		exports.GTWtopbar:dm("Negative amounts are not allowed", player, 255, 0, 0)
	elseif not getPlayerTeam(player) then
		exports.GTWtopbar:dm("You must be in a team in order to send money, please reconnect if youre not!", player, 255, 0, 0)
	else
		exports.GTWtopbar:dm("Correct syntax: /give <player-nick> <amount>", player, 255, 255, 255)
	end
end
addCommandHandler("give", sendMoneyToPlayer)

  

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...