Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/05/23 in all areas

  1. Hello my name is : Shady,I will show you some examples and tutorials that can be done with the math method. Lua provides a set of built-in math functions that can be used to perform various mathematical operations. Here are some of the most commonly used functions: math.abs(x) This function returns the absolute value of a given number x. Example cd: local x = -10 local abs_x = math.abs(x) -- abs_x is 10 math.ceil(x) This function rounds up a given number x to the nearest integer. Example cd: local x = 3.5 local ceil_x = math.ceil(x) -- ceil_x is 4 math.floor(x) This function rounds down a given number x to the nearest integer. Example cd: local x = 3.5 local floor_x = math.floor(x) -- floor_x is 3 math.max(x1, x2, ...) This function returns the maximum value among the given numbers. Example cd: local max_value = math.max(10, 20, 30) -- max_value is 30 math.min(x1,x2, ...) This function returns the minimum value among the given numbers. Example cd: local min_value = math.min(10, 20, 30) -- min_value is 10 math.random([m [, n]]) This function returns a random number between m and n. If m and n are not provided, it returns a random number between 0 and 1. Example cd: local random_value = math.random(1, 100) -- random_value is a random number between 1 and 100 math.sqrt(x) This function returns the square root of a given number x. Example cd: local x = 16 local sqrt_x = math.sqrt(x) -- sqrt_x is 4 In Lua, math.huge is a special value that represents positive infinity. It is often used in mathematical calculations where a number needs to be compared or used as a placeholder for a very large number. For example, let's say you want to find the maximum value in an array of numbers. You could start by initializing a variable max to -math.huge, which is the smallest possible number in Lua. Then you could loop through the array and compare each element to max, updating max if the element is larger. By starting with -math.huge, you can be sure that the first element in the array will always be larger than max, allowing you to update max with the first element. Here's an example code snippet that demonstrates the use of math.huge: local numbers = {5, 10, 2, 7, 15} local max = -math.huge for _, number in ipairs(numbers) do if number > max then max = number end end print("The maximum value is:", max) In this code, we start by defining an array of numbers. Then we initialize max to -math.huge before looping through the array using ipairs. For each number in the array, we check if it is greater than the current value of max. If it is, we update max with the new value. Finally, we print out the maximum value. Here are some examples of how these functions can be used in MTA scripts: Example 1: Calculating the distance between two points. getDistanceBetweenPoints2D function getDistanceBetweenPoints2D(x1, y1, x2, y2) local dx = x2 - x1 local dy = y2 - y1 return math.sqrt(dx*dx + dy*dy) end local distance = getDistanceBetweenPoints2D(10, 20, 30, 40) -- distance is the distance between the two points (10,20) and (30,40) Example 2: Generating a random number outputChatBox local random_value = math.random(1, 100) outputChatBox("The random number is: " .. random_value) Here I will show you 5 script sample codes with MTA Lua math function. In this direction, you can find some methods and information on how to create scripts with the math function. 1-) Generating Random Colors: string-methods function generateRandomColor() -- Generate random numbers from 0 to 255 local r = math.random(0, 255) local g = math.random(0, 255) local b = math.random(0, 255) -- return color code return string.format("#%02X%02X%02X", r, g, b) end 2-) Calculating the Distance Between Players: getElementPosition getDistanceBetweenPoints3D function getPlayerDistance(player1, player2) -- get the positions of the players local x1, y1, z1 = getElementPosition(player1) local x2, y2, z2 = getElementPosition(player2) -- calculate the distance local distance = getDistanceBetweenPoints3D(x1, y1, z1, x2, y2, z2) -- return the distance return distance end 3-) Calculating Vehicle Speed: getElementVelocity function getVehicleSpeed(vehicle) -- get the speed of the vehicle local speedX, speedY, speedZ = getElementVelocity(vehicle) -- Calculate the magnitude of the velocity vector local speed = math.sqrt(speedX^2 + speedY^2 + speedZ^2) -- return speed return speed end 4-) Showing Player's Health in Percentage: getElementHealth function getPlayerHealthPercentage(player) -- get player's health value local health = getElementHealth(player) -- Calculate percentage of health value local healthPercent = math.floor((health / 100) * 100) -- return health percentage return healthPercent end 5-) Calculating the Two-Dimensional Distance of Players: getElementPosition function getPlayer2DDistance(player1, player2) -- get the positions of the players local x1, y1, _ = getElementPosition(player1) local x2, y2, _ = getElementPosition(player2) -- calculate the distance local distance = math.sqrt((x2 - x1)^2 + (y2 - y1)^2) -- return the distance return distance end As you can see, here are 5 script examples with the MTA Lua math function
    3 points
  2. hello, im trying to make a bank system and when i try to withdraw the money it doesnt take away my money in the bank, this is the client side script loadstring(exports.dgs:dgsImportFunction())() local windows = window,label1,label2,button1,button2 sx, sy = dgsGetScreenSize() easing = "OutBack" --data setElementData(localPlayer, "bank", "100") bank11 = getElementData (localPlayer, "bank") --window window = dgsCreateImage(sx*0.360, sy*0.3, sx*0.330, sy*0.2,_,false,_, tocolor(0,0,0,140)) label1 = dgsCreateImage(sx*0.360, sy*0.270, sx*0.330, sy*0.030,_,false,_, tocolor(0,255,255,100)) label2 = dgsCreateLabel(sx*0.450, sy*0.270, sx*0.330, sy*0.030, "Bank !!") dgsSetProperty(label2,"textSize",{sx*0.004, sy*0.002}) -- buttons button1 = dgsCreateButton(sx*0.370, sy*0.360, sx*0.150, sy*0.080, "kivetel",false, _, _, _,_,_, _,_,tocolor(0,255,255,100), tocolor(0,255,255,150), tocolor(0,255,255,255)) button2 = dgsCreateButton(sx*0.530, sy*0.360, sx*0.150, sy*0.080, "befektetes",false, _, _, _,_,_, _,_,tocolor(255,255,255,100), tocolor(255,255,255,150), tocolor(255,255,255,255)) --animations dgsSetAlpha(window, 0) dgsAlphaTo(window, 1, "OutQuad", 1000) dgsSetAlpha(label1, 0) dgsAlphaTo(label1, 1, "OutQuad", 1000) dgsSetAlpha(label2, 0) dgsAlphaTo(label2, 1, "OutQuad", 1000) dgsSetAlpha(button1, 0) dgsAlphaTo(button1, 1, "OutQuad", 1000) dgsSetAlpha(button2, 0) dgsAlphaTo(button2, 1, "OutQuad", 1000) -- button func 1 addEventHandler("onDgsMouseClickDown", button1, function() dgsSetVisible(window, false) dgsSetVisible(label1, false) dgsSetVisible(label2, false) dgsSetVisible(button1, false) dgsSetVisible(button2, false) window2 = dgsCreateImage(sx*0.360, sy*0.3, sx*0.330, sy*0.2,_,false,_, tocolor(0,0,0,140)) label3 = dgsCreateImage(sx*0.360, sy*0.270, sx*0.330, sy*0.030,_,false,_, tocolor(0,255,255,100)) label4 = dgsCreateLabel(sx*0.360, sy*0.320, sx*0.330, sy*0.030, "a penzed a bankba : " .. tostring(bank11).. "$") dgsSetProperty(label4,"textSize",{sx*0.002, sy*0.002}) button3 = dgsCreateButton(sx*0.450, sy*0.360, sx*0.150, sy*0.080, "kivetel",false, _, _, _,_,_, _,_,tocolor(0,255,255,100), tocolor(0,255,255,150), tocolor(0,255,255,255)) dgsSetAlpha(label4, 0) dgsAlphaTo(label4, 1, "OutQuad", 1000) addEventHandler("onDgsMouseClickDown", button3, function() if bankmoney == 0 then dgsSetVisible(window, true) dgsSetVisible(label1, true) dgsSetVisible(label2, true) dgsSetVisible(button1, true) dgsSetVisible(button2, true) dgsSetVisible(window2, false) dgsSetVisible(label3, false) dgsSetVisible(label4, false) dgsSetVisible(button3, false) else givePlayerMoney(bank11, localPlayer) setElementData(localPlayer, "bank", "-100") end end ) end ) this is the server side script createObject(2942, -30.522729873657 , -1.5657294988632 , 2.70) createColSphere(-30.522729873657 , -1.90 , 3.05, 1)
    1 point
  3. 1 point
×
×
  • Create New...