Hi there everyone!

I use a resource to save coords for use in other resources the problem I have is that I need to be able to save them as:

999.99

But my resource saves them as:

999.99999999999

I’d like to handle it on the client side so that I can use it both for display in the game and saving to a text file.

Here’s my current snippet:

AddEventHandler("SaveCommand", function()
		x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
		heading = GetEntityHeading(GetPlayerPed(-1))
	    local PlayerName = GetPlayerName()
	    TriggerServerEvent("SaveCoords", PlayerName , x , y , z, heading)
	    TriggerEvent("chatMessage", "SYSTEM", {255, 0, 0}, "Your coords are X:" .. x .. ", Y:" .. y .. ", Z:" .. z.." H:"..heading)
end)

Could someone help me figure out how to convert the coordinates to only two decimal places?

Thanks for your time!

function roundToNthDecimal(num, n)
	local mult = 10^(n or 0)
	return math.floor(num * mult + 0.5) / mult
end
TriggerEvent('chat:addMessage', { args = { ('Coordinates: X: %.2f, Y: %.2f, Z: %.2f, H: %.2f'):format(x, y, z, heading) } })

Alternately, ('...'):format(...) is equivalent to string.format('...', ...).

See also the documentation on string.format.

1 Like

Hi there, Scammer and thanks for the help!

I tried implementing this but have done something incorrectly, I think. Here’s my attempt:

RegisterNetEvent("SaveCommand")

function roundToNthDecimal(num, n)
	local mult = 10^(n or 0)
	return math.floor(num * mult + 0.5) / mult
end

AddEventHandler("SaveCommand", function()
		x, y, z = table.unpack(roundToNthDecimal(GetEntityCoords(GetPlayerPed(-1), true)))
		heading = GetEntityHeading(GetPlayerPed(-1))
	    local PlayerName = GetPlayerName()
	    TriggerServerEvent("SaveCoords", PlayerName , x , y , z, heading)
	    TriggerEvent("chatMessage", "SYSTEM", {255, 0, 0}, "Your coords are X:" .. x .. ", Y:" .. y .. ", Z:" .. z.." H:"..heading)
end)

But this is saving coords with a single zero as the decimal space, no matter where I stand.

I tried passing an argument for n as well, which caused the script to revert to full coords:

x, y, z = table.unpack(roundToNthDecimal(GetEntityCoords(GetPlayerPed(-1), true), 2))

Did I implement it incorrectly or is there perhaps a change I should have made to the code to get what I want?

Hi there and thanks very much for the help!

This solves my problem on the client side where it prints to screen but doesn’t resolve it on the server side where I save to a text file. That’s why I was hoping to solve it previously in the code, so it would send the truncated numbers to the server side.

This is my server side:

RegisterServerEvent("SaveCoords")
AddEventHandler("SaveCoords", function( PlayerName , x , y , z, heading )
 file = io.open( PlayerName .. "-Coords.txt", "a")
    if file then
        
        file:write("new Vector3("..x.."f, "..y.."f, "..z.."f),\n")
        
    end
    file:close()
end)

Hi there again,

I’m playing with this but noticing that my heading always returns as H:0.00 . If I remove the string format, it shows correctly(but long). Do I need to change something in the code to perhaps get it to handle heading correctly?

RegisterNetEvent("SaveCommand")

AddEventHandler("SaveCommand", function()
		x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
		heading = GetEntityHeading(GetPlayerPed(-1))
	    local PlayerName = GetPlayerName()
	    TriggerServerEvent("SaveCoords", PlayerName , string.format('%.2f', x) , string.format('%.2f', y) , string.format('%.2f', z), string.format('%.2f', heading))
	   TriggerEvent('chat:addMessage', { args = { ('Coordinates: X: %.2f, Y: %.2f, Z: %.2f, H: %.2f'):format(x, y, z, heading) } })
end)

Put this clientside, for each decimal you want, add a 0 to both the 100s below. You can also change the Citizen.Trace which is inside your F8 key to your chat message instead.

function tD(n)
    n = math.ceil(n * 100) / 100
    return n
end

RegisterNetEvent("SaveCommand")
AddEventHandler("SaveCommand", function()
	x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
	local PlayerName = GetPlayerName()
	Citizen.Trace(""..tD(x)..","..tD(y)..","..tD(z)..","..tD(GetEntityHeading(GetPlayerPed(-1))).."")
	TriggerServerEvent( "SaveCoords", PlayerName , tD(x) , tD(y) , tD(z), tD(GetEntityHeading(GetPlayerPed(-1))) )			
end)

Put this serverside

RegisterServerEvent("SaveCoords")
AddEventHandler("SaveCoords", function( PlayerName , x , y , z, h )
    file = io.open( PlayerName .. "-Coords.txt", "a")
    if file then
        file:write("{" .. x .. "," .. y .. "," .. z .. "," .. h .. "}")
        file:write("\n")
    end
    file:close()
end)
1 Like