Jump to content

Animation of giving money


Recommended Posts

Hello! I tried to do something similar. It's not the best way, but it works as it should.

Try it (server):

local delay = 20

function moneyChange(player, commandName, count)
	count = tonumber(count)
	if type(count) ~= "number" then return end
	
	local operaceType = "give"
	if count < 0 then 
		operaceType = "take"
		count = -count
	end

	local addMoney = 0
	local modf = 1
	if count > 100 then modf = modf*10 end
	if count > 1000 then modf = modf*10 end
	if count > 10000 then modf = modf*10 end
	if count > 100000 then modf = modf*10 end
	if count > 1000000 then modf = modf*10 end

	local timer = setTimer(function()
		addMoney = math.random(1,modf)
		count = count - addMoney
		if count < 1000000 and modf == 100000 then modf = modf/10 end
		if count < 100000 and modf == 10000 then modf = modf/10 end
		if count < 10000 and modf == 1000 then modf = modf/10 end
		if count < 1000 and modf == 100 then modf = modf/10 end
		if count < 100 and modf == 10 then modf = modf/10 end
		if count <= 0 then 
			if operaceType == "give" then
				givePlayerMoney(player, count + addMoney)
			else
				takePlayerMoney(player, count + addMoney)
			end
			count = 0 
			if isTimer(timer) then
				killTimer(timer)
				timer = nil
			end
			return
		end

		if operaceType == "give" then
			givePlayerMoney(player, addMoney)
		else
			takePlayerMoney(player, addMoney)
		end
	end, delay, 0)
	
end
addCommandHandler("money", moneyChange)
Link to comment
  • Moderators
46 minutes ago, Molvine said:

I tried to do something similar.

Looks nice! Just some side notes, feel free to ignore those.

 

I recommend not do these kind of animations serverside. You are basically sending data to the client/player every 20 milliseconds, which might causing network trouble on a not local server. Just set the end-value serverside. And trigger an even clientside + do the animation there.

 

Also the GTA default money animation can be displayed with setPlayerMoney (unless of course you want to use a custom transition): https://wiki.multitheftauto.com/wiki/SetPlayerMoney

  • Thanks 1
Link to comment
2 hours ago, IIYAMA said:

Выглядит хорошо! Просто некоторые примечания, не стесняйтесь их игнорировать.

 

Я рекомендую не делать такие анимации на стороне сервера . Вы в основном отправляете данные клиенту/игроку каждые 20 миллисекунд, что может вызвать проблемы с сетью на не локальном сервере. Просто установите конечное значение на стороне сервера. И запустить даже клиентскую часть + сделать там анимацию.

 

Также анимацию денег GTA по умолчанию можно отобразить с помощью setPlayerMoney (если, конечно, вы не хотите использовать пользовательский переход): https://wiki.multitheftauto.com/wiki/SetPlayerMoney

Thank you very much for your advice! I thought that he would not ask such a question, since the default GTA HUD has such an animation by default, and did it right on the user interface.

Here is the corrected code:
Client:
 

local delay = 20

function moneyChange(commandName, count)
	count = tonumber(count)
	if type(count) ~= "number" then return end
	
	local operaceType = "give"
	if count < 0 then 
		operaceType = "take"
		count = -count
	end

	local addMoney = 0
	local modf = 1
	if count > 100 then modf = modf*10 end
	if count > 1000 then modf = modf*10 end
	if count > 10000 then modf = modf*10 end
	if count > 100000 then modf = modf*10 end
	if count > 1000000 then modf = modf*10 end

	local timer = setTimer(function()
		addMoney = math.random(1,modf)
		count = count - addMoney
		if count < 1000000 and modf == 100000 then modf = modf/10 end
		if count < 100000 and modf == 10000 then modf = modf/10 end
		if count < 10000 and modf == 1000 then modf = modf/10 end
		if count < 1000 and modf == 100 then modf = modf/10 end
		if count < 100 and modf == 10 then modf = modf/10 end
		if count <= 0 then 
			triggerServerEvent("money:moneyChange", resourceRoot, localPlayer, count + addMoney, operaceType)
			count = 0 
			if isTimer(timer) then
				killTimer(timer)
				timer = nil
			end
			return
		end

		triggerServerEvent("money:moneyChange", resourceRoot, localPlayer, addMoney, operaceType)
	end, delay, 0)
	
end
addCommandHandler("money", moneyChange)

addEventHandler("onClientRender", root, function()
	dxDrawText(tostring(getPlayerMoney(localPlayer)), 500, 300, _, _, _, 3)
end)

Server:
 

addEvent("money:moneyChange", true)
addEventHandler("money:moneyChange", root, function(player, count, operaceType)
	if not player or not count or not operaceType then return end
	if operaceType == "give" then
		givePlayerMoney(player, count)
	else
		takePlayerMoney(player, count)
	end
end)

 

Edited by Molvine
  • Like 2
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...