Jump to content

[Help] Give and take money to specific player


SinaAmp

Recommended Posts

Hello guys

I want to give and take money from a specific player and update database data for that player using command

i wrote my script but that give money only to me

here is my script:

function moneyCmd(player, commandName, AccName, amount)
		if(getElementType(player) ~= "player") then return end 
		pacc = getPlayerAccount (player)
		AccName = getAccountName (pacc)
		if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic
			if commandName == "givemoney" then
				amount  = tonumber(amount)
				if tostring(AccName) and amount then
					local PMoney = givePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName)
				else
					outputChatBox("[usage] /givemoney [playername] [amount]", player)
				end
			else if commandName == "takemoney" then
				amount = tonumber(amount)
				if tostring(AccName) and amount then
					takePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName)
				else
					outputChatBox("[usage] /takemoney [playername] [amount]", player)
				end
			else 
				outputChatBox("kolan ridi", player)
			end
			end
		else
			outputChatBox("You aren't able to use this command", player)
		end
	end
	 
	addCommandHandler("givemoney", moneyCmd);
	addCommandHandler("takemoney", moneyCmd);

 

Link to comment

edited a little bit:

Spoiler
function moneyCmd(player, commandName, AccName, amount)
		if(getElementType(player) ~= "player") then return end 
		pacc = getPlayerAccount (player)
		AccName = getAccountName (pacc)
		if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic
			if commandName == "givemoney" then
				amount  = tonumber(amount)
				if tostring(AccName) and amount then
					local playermoney = getPlayerMoney(player)
					local sumpmoney = tonumber(playermoney) + tonumber(amount)
					givePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",sumpmoney,AccName)
				else
					outputChatBox("[usage] /givemoney [playername] [amount]", player)
				end
			else if commandName == "takemoney" then
				amount = tonumber(amount)
				if tostring(AccName) and amount then
					local playermoney = getPlayerMoney(player)
					local subpmoney = tonumber(playermoney) - tonumber(amount)
					takePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",subpmoney,AccName)
				else
					outputChatBox("[usage] /takemoney [playername] [amount]", player)
				end
			else 
				outputChatBox("kolan ridi", player)
			end
			end
		else
			outputChatBox("You aren't able to use this command", player)
		end
	end
	 
	addCommandHandler("givemoney", moneyCmd);
	addCommandHandler("takemoney", moneyCmd);

 

 

Link to comment
1 hour ago, SinaAmp said:

edited a little bit:

  Hide contents
function moneyCmd(player, commandName, AccName, amount)
		if(getElementType(player) ~= "player") then return end 
		pacc = getPlayerAccount (player)
		AccName = getAccountName (pacc)
		if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic
			if commandName == "givemoney" then
				amount  = tonumber(amount)
				if tostring(AccName) and amount then
					local playermoney = getPlayerMoney(player)
					local sumpmoney = tonumber(playermoney) + tonumber(amount)
					givePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",sumpmoney,AccName)
				else
					outputChatBox("[usage] /givemoney [playername] [amount]", player)
				end
			else if commandName == "takemoney" then
				amount = tonumber(amount)
				if tostring(AccName) and amount then
					local playermoney = getPlayerMoney(player)
					local subpmoney = tonumber(playermoney) - tonumber(amount)
					takePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",subpmoney,AccName)
				else
					outputChatBox("[usage] /takemoney [playername] [amount]", player)
				end
			else 
				outputChatBox("kolan ridi", player)
			end
			end
		else
			outputChatBox("You aren't able to use this command", player)
		end
	end
	 
	addCommandHandler("givemoney", moneyCmd);
	addCommandHandler("takemoney", moneyCmd);

 

 

local playermoney = getPlayerMoney(player)
Player in this case is the source that is executing it, so you.

If you want to set it to a player with an account that is currently logged in you should do that with a loop.

for k,v in pairs(getElementsByType("player")) do
	if getAccountName(v) == AccName then -- This can cause problems if more players are on the same account, giving all of them money
    	givePlayerMoney(v, amount)
   	end
end

 

Edited by Derbo
  • Like 1
Link to comment

 

I'm not sure but can you test this code the problem might be that you assign your own account to your AccName variable in the first place

function moneyCmd(player, commandName, AccName, amount)
		--if(getElementType(player) ~= "player") then return end -- not need this
		--pacc = getPlayerAccount (player) -- You assign your own account to the accName variable
		--AccName = getAccountName (pacc) -- try remove this
		local targetAccount = getAccount(AccName) -- get account from AccName
		if not (targetAccount) then return outputChatBox("There is no such account in the database.", player) end --Check if there is an account named accName, if not, don't continue
		
		if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic
			if commandName == "givemoney" then
				amount  = tonumber(amount)
				if tostring(AccName) and amount then
					local PMoney = givePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName)
				else
					outputChatBox("[usage] /givemoney [playername] [amount]", player)
				end
			else if commandName == "takemoney" then
				amount = tonumber(amount)
				if tostring(AccName) and amount then
					takePlayerMoney(player, amount)
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName)
				else
					outputChatBox("[usage] /takemoney [playername] [amount]", player)
				end
			else 
				outputChatBox("kolan ridi", player)
			end
			end
		else
			outputChatBox("You aren't able to use this command", player)
		end
	end
	 
	addCommandHandler("givemoney", moneyCmd);
	addCommandHandler("takemoney", moneyCmd);
Edited by Burak5312
  • Like 1
Link to comment

do you mean clculate that in dbexec parameter? @Derbo

thank you bro @Burak5312 i fixed that before:

Spoiler
function moneyCmd(player, commandName, InAccName, amount)
	if(getElementType(player) ~= "player") then return end 
	pacc = getPlayerAccount (player)
	AccName = getAccountName (pacc)
	if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- if player is in acl group
		if commandName == "givemoney" then
			local amount  = tonumber(amount)
			if amount then
				for k,v in pairs(getElementsByType("player")) do
					if getAccountName(pacc) == InAccName then 
						local playermoney = getPlayerMoney(player)
				        local sumpmoney = tonumber(playermoney) + tonumber(amount)
				        dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",sumpmoney,AccName)
						givePlayerMoney(v, amount)
					end
				end
				
			else
				outputChatBox("[usage] /givemoney [playername] [amount]", player)
			end
		else if commandName == "takemoney" then
			local amount = tonumber(amount)
			if amount then
				for k,v in pairs(getElementsByType("player")) do
					if getAccountName(pacc) == InAccName then 
						local playermoney = getPlayerMoney(player)
				        local subpmoney = tonumber(amount) - tonumber(playermoney)
				        dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",subpmoney,AccName)
						takePlayerMoney(v, amount)
					end
				end
			else
				outputChatBox("[usage] /takemoney [playername] [amount]", player)
			end
		else 
			outputChatBox("kolan ridi", player)
		end
		end
	else
		outputChatBox("You aren't able to use this command", player)
	end
end
 
addCommandHandler("givemoney", moneyCmd);
addCommandHandler("takemoney", moneyCmd);

 

but i want to change money of the offline players and now i working on that

Edited by SinaAmp
  • Like 1
Link to comment

 

Can you try this, sorry for the messy codes, the forum is breaking it

function moneyCmd(player, commandName, AccName, amount)
		--if(getElementType(player) ~= "player") then return end -- not need this
		--pacc = getPlayerAccount (player) -- You assign your own account to the accName variable
		--AccName = getAccountName (pacc) -- try remove this
		local targetAccount = getAccount(AccName) -- get account from AccName
		if not (targetAccount) then return outputChatBox("There is no such account in the database.", player) end --Check if there is an account named accName, if not, don't continue
		
		if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic
		    local targetMoney
		    local MoneyData = dbPoll(dbQuery(db,"SELECT Money FROM stats WHERE Account=? LIMIT 1",AccName), -1)  
            if(MoneyData) then
               if(#MoneyData > 0) then
                  for _,row in ipairs(MoneyData) do
                  	 targetMoney = row["Money"] -- get player money amount from database
                  	 break
                  end
               end
            end

			if commandName == "givemoney" then
				amount  = tonumber(amount)
				if tostring(AccName) and amount then
					targetMoney = targetMoney + amount -- addition with amount
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?", targetMoney,AccName)
				else
					outputChatBox("[usage] /givemoney [playername] [amount]", player)
				end
			else if commandName == "takemoney" then
				amount = tonumber(amount)
				if tostring(AccName) and amount then
					targetMoney = targetMoney - amount -- subtraction with amount
					dbExec(db,"UPDATE stats SET Money=? WHERE Account=?", targetMoney,AccName)
				else
					outputChatBox("[usage] /takemoney [playername] [amount]", player)
				end
			else 
				outputChatBox("kolan ridi", player)
			end
			end
		else
			outputChatBox("You aren't able to use this command", player)
		end
	end
	 
	addCommandHandler("givemoney", moneyCmd);
	addCommandHandler("takemoney", moneyCmd);
Edited by Burak5312
  • Thanks 1
Link to comment

hello bro @Burak5312

again me ?

i want to save player health after player quit but nothing change in database

function phealth (_,account)
	local AccName = getAccountName(account)
	local playerhealth = getElementHealth(source)
	dbExec(db,"UPDATE stats SET Health=? WHERE Account=?",playerhealth, AccName)
end
addEventHandler("onplayerQuit", root, phealth)

 

Link to comment
function phealth () -- onPlayerQuit event does not keep accounts
    local playerAccount = getPlayerAccount(source)
    if (playerAccount) then
       if(isGuestAccount(playerAccount)) then return end -- if guest account don't continue
    end
	local AccName = getAccountName(playerAccount)
	local playerhealth = getElementHealth(source)
	dbExec(db,"UPDATE stats SET Health=? WHERE Account=?",playerhealth, AccName)
end
addEventHandler("onPlayerQuit", root, phealth) --there is a typo here

 

  • 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...