Matevsz Posted April 15 Share Posted April 15 Hello everyone. How can I make it add smoothly when adding an amount, e.g. $10? $1, $2, $3, $4... and not $10 right away? hud = {} moneyHUD = {"money"} hud.font = dxCreateFont("font.ttf", 32.5, false, "antialiased") addEventHandler("onClientResourceStart", getResourceRootElement, function() setPlayerHudComponentVisible(moneyHUD, false) end) addEventHandler("onClientRender", root, funtion() local money = getPlayerMoney(localPlayer) dxDrawText("$000000000", 1305, 278, 1598, 328, tocolor(255, 255, 255, 255), 1.00, hud.font, "center", "center", false, false, false, false, false) end) Link to comment
Matevsz Posted April 16 Author Share Posted April 16 (edited) 10 hours ago, FileEX said: Use interpolateBetween hud={} moneyHUD={"money"} hud.font = dxCreateFont("font.ttf", 32.5, false, "antialiased") local lastMoney = 0 local moneyChangeStartTime = 0 local moneyChangeDuration = 2000 addEventHandler("onClientResourceStart", getResourceRootElement, function() setPlayerHudComponentVisible(moneyHUD, false) end) addEventHandler("onClientRender", root, function() local money = getPlayerMoney(localPlayer) money = math.min(money, 99999999) if money ~= lastMoney then lastMoney = money moneyChangeStartTime = getTickCount() end local elapsedTime = getTickCount() - moneyChangeStartTime local progress = elapsedTime / moneyChangeDuration local money = interpolateBetween(0, 0, 0, money, 0, 0, progress, "Linear") dxDrawText("$00000000", 1305, 278, 1598, 328, tocolor(255, 255, 255, 255), 1.00, hud.font, "center", "center", false, false, false, false, false) if elapsedTime >= moneyChangeDuration then lastMoney = money end end) adds money smoothly, but when you add an amount greater than the maximum (99999999) it starts adding again from zero Edited April 16 by Matevsz Link to comment
FileEX Posted April 16 Share Posted April 16 (edited) As you can see, you start the interpolation from 0. Write down the current balance of money before you start the interpolation and start from there. You can use e.g. a ticks to see if the amount of money has changed local fromMoney, toMoney, visibleMoney = 0,0,0; local moneyCheckTick,moneyAnimTick = getTickCount(); if (moneyCheckTick and not moneyAnimTick) then if (getTickCount() - moneyCheckTick > 1000) then -- check every 1 s local money = getPlayerMoney(localPlayer); if (visibleMoney ~= money) then moneyAnimTick = getTickCount(); fromMoney, toMoney = visibleMoney, money; end moneyCheckTick = getTickCount(); end end if (moneyAnimTick) then local progress = (getTickCount() - moneyAnimTick) / 1000; visibleMoney = interpolateBetween(fromMoney, 0, 0, toMoney, 0, 0, progress, 'Linear'); if (progress > 1) then moneyAnimTick = nil; end end -- dxDrawText for visibleMoney variable Sorry for the broken indentation in the code, but the code editor on the forum is strange and I don't know why it happens. Edited April 16 by FileEX Link to comment
Matevsz Posted April 17 Author Share Posted April 17 18 hours ago, FileEX said: As you can see, you start the interpolation from 0. Write down the current balance of money before you start the interpolation and start from there. You can use e.g. a ticks to see if the amount of money has changed local fromMoney, toMoney, visibleMoney = 0,0,0; local moneyCheckTick,moneyAnimTick = getTickCount(); if (moneyCheckTick and not moneyAnimTick) then if (getTickCount() - moneyCheckTick > 1000) then -- check every 1 s local money = getPlayerMoney(localPlayer); if (visibleMoney ~= money) then moneyAnimTick = getTickCount(); fromMoney, toMoney = visibleMoney, money; end moneyCheckTick = getTickCount(); end end if (moneyAnimTick) then local progress = (getTickCount() - moneyAnimTick) / 1000; visibleMoney = interpolateBetween(fromMoney, 0, 0, toMoney, 0, 0, progress, 'Linear'); if (progress > 1) then moneyAnimTick = nil; end end -- dxDrawText for visibleMoney variable Sorry for the broken indentation in the code, but the code editor on the forum is strange and I don't know why it happens. No problem, nothing happened. Now it adds and subtracts money smoothly, but after exceeding the maximum amount (99999999) the amount is 100000000. I added it to the code (math.min) Link to comment
FileEX Posted April 18 Share Posted April 18 I don't understand what the problem is. If you exceed the maximum amount, it is normal that the number is higher Link to comment
Matevsz Posted April 18 Author Share Posted April 18 1 hour ago, FileEX said: I don't understand what the problem is. If you exceed the maximum amount, it is normal that the number is higher When I did not use the interpolateBetween function, after adding an amount larger than the maximum, e.g. 111111111, the amount appeared 99999999 and with the interpolateBetween function the animation ends at 100000000 Link to comment
FileEX Posted April 19 Share Posted April 19 Sometimes interpolateBetween does some weird rounding. Try manual interpolation money = lastMoney + (newMoney - lastMoney) * progress; Or use what Tekken provided Link to comment
DoliDoll Posted April 20 Share Posted April 20 If It was a roleplay server use Owl Gaming to do it easily 1 Link to comment
Matevsz Posted April 20 Author Share Posted April 20 I used the above examples and now in the code: hud = {} moneyHUD = {"money"} hud.font = dxCreateFont("font.ttf", 32.5, false, "antialiased") local moneyMin = 0 local moneyMax = 99999999 local currentMoney = getPlayerMoney(localPlayer) addEventHandler("onClientResourceStart", resourceRoot, function() setPlayerHudComponentVisible(moneyHUD[1], false) end) function lerp(a, b, t) return a + (b - a) * t end addEventHandler("onClientRender", root, function() local money = getPlayerMoney(localPlayer) money = math.max(moneyMin, math.min(moneyMax, money)) currentMoney = lerp(currentMoney, money, 0.05) -- dxDrawText line end) after adding money, e.g. 25555, the player gets 25554 instead of 25555 Link to comment
Tekken Posted April 21 Share Posted April 21 On 19/04/2024 at 08:05, Tekken said: math.max(YOUR_MONEY, 999999999); My bad you actually have to use math.min not math.max Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now