Jump to content

Need help with my scipt


XJMLN

Recommended Posts

Hello guys i write script, everything works fine, but if there 2-3 the last time, 3d-text show doubles time(http://imgur.com/cSI4ub5) How i can fix that?

Sorry for bad english. :P

  
local drag = {} 
  
local deftext=" Last time:" 
  
local t = createElement("text") 
setElementPosition(t,2225.85,-1742.41,15) 
setElementData(t,"text",deftext) 
setElementData(t,"scale",1.0) 
  
local function addText() 
    local t = createElement("text") 
    setElementPosition(t,2225.85,-1742.41,14.56) 
    setElementData(t,"text",table.concat(drag)) 
    setElementData(t,"scale",1) 
    setElementData(t,"1stAdd",true) 
    end 
function registered(plr,vehicleP,timeP) 
    if vehicleP and timeP then 
        local text = getPlayerName(plr).."-"..getVehicleNameFromModel(vehicleP).." time: "..string.format("%.2f",timeP).."\n" 
        if text then 
        table.insert(drag,text) 
        addText() 
    end 
    end 
        if #drag > 5 then 
            table.remove(drag, 1) 
        end --addText() 
end 
addEvent("registerPredkosc",true) 
addEventHandler("registerPredkosc",resourceRoot,registered) 

Link to comment

from 3d-text:

-- based on 3dtext by The_Kid 
defscale=1 
defr=255 
defg=255 
defb=255 
defa=255 
defdd=100 
defscale3d=true 
deffont= dxCreateFont("opensanslight.ttf", 12) 
  
  
local sx, sy     = guiGetScreenSize ( ) 
local gx         = sx * 0.1 
local gy         = sy * 0.1 
  
local posX    = 0 
local posY    = 0 
local sizeX   = 0 
local sizeY   = 0 
  
local pd,pi 
  
local teksty={} 
  
function refreshNearbyTexts() 
 teksty=getElementsByType ( "text", getRootElement(), true ) 
 local px,py,_ = getCameraMatrix() 
  
 pd=getElementDimension(localPlayer) 
 pi=getElementInterior(localPlayer) 
  
 for i, t in pairs ( teksty  ) do 
    if (getElementDimension(t)~=pd or getElementInterior(t)~=pi) then 
        teksty[i]=nil 
    else 
        local cx, cy, _ = getElementPosition ( t ) 
        local dist = getDistanceBetweenPoints2D ( px, py, cx, cy) 
        if (dist>defdd) then 
            teksty[i]=nil 
        end 
    end 
 end 
  
end 
  
setTimer(refreshNearbyTexts, 2000, 0) 
  
  
addEventHandler ( "onClientRender", getRootElement ( ), 
function ( ) 
 local px,py,pz = getCameraMatrix() 
 for i, t in pairs ( teksty ) do 
    if (t and isElement(t) and getElementDimension(t)==pd and getElementInterior(t)==pi) then 
            local cx, cy, cz = getElementPosition ( t ) 
             
            if ( getElementData(t, "attach") ) and (not getElementPosition(getElementData(t, "attach"))) then destroyElement(t) end 
            if getElementData(t, "attach") and isElement(getElementData(t, "attach")) and getElementType(getElementData(t, "attach"))=="ped" then 
                cx,cy,cz = getElementPosition(getElementData(t, "attach")) 
            end 
             
            if getElementData(t, "addz") then 
                cz = cz+getElementData(t, "addz") or cz 
            end 
             
            local dist = getDistanceBetweenPoints3D ( px, py, pz, cx, cy, cz ) 
            if dist < defdd then 
                local scx, scy = getScreenFromWorldPosition ( cx, cy, cz, 100, true ) 
                if scx and scy and isLineOfSightClear ( px, py, pz, cx, cy, cz, true,false, true, true ) then 
                    --The text default parameters 
                    local alpha   = defa 
                    local r       = defr 
                    local g       = defg 
                    local b       = defb 
                    local scale   = defscale 
                    local scale3d = defscale3d 
                    local font = deffont 
                    if getElementData ( t, "scale", false ) then scale = getElementData ( t, "scale", false ) end 
                    if getElementData ( t, "font", false ) then font = getElementData ( t, "font", false) end 
                    if scale3d == true then scale = scale * ( ( defdd - dist ) / defdd ) end 
                    local text  = getElementData ( t, "text" ) 
                    if not text or text == "" then return end 
                    local c     = getElementData ( t, "rgba" ) 
                    if c and type ( c ) == "table" then r = c[1] g = c[2] b = c[3] alpha = c[4] end 
                    local ac=(r+g+b)/3 
                    dxDrawText ( text, scx+2, scy+2, scx, scy, tocolor ( 255-ac, 255-ac, 255-ac, alpha ), scale, font, "center", "center" ) 
                    dxDrawText ( text, scx-2, scy+2, scx, scy, tocolor ( 255-ac, 255-ac, 255-ac, alpha ), scale, font, "center", "center" ) 
                    dxDrawText ( text, scx+2, scy-2, scx, scy, tocolor ( 255-ac, 255-ac, 255-ac, alpha ), scale, font, "center", "center" ) 
                    dxDrawText ( text, scx-2, scy-2, scx, scy, tocolor ( 255-ac, 255-ac, 255-ac, alpha ), scale, font, "center", "center" ) 
                    dxDrawText ( text, scx, scy, scx, scy, tocolor ( r, g, b, alpha ), scale, font, "center", "center" ) 
                end 
            end 
    end 
 end 
end ) 
  
  

Link to comment

This is the script to record(?) the time from the start line to the finish line, after crossing the finish line executes the up script, if only 1 player end this record is okay - the text is displayed right. But when next player - first the contents of the table is doubled.

Screenshot maybe help u in explain ;d

if there only 1 content in table - http://imgur.com/X5cUNH

if there 2 content in table - http://imgur.com/2wKsYYU

Link to comment

How are you calling "registerPredkosc", since you're giving the function a player element as first argument ( plr ). If you're calling this for every single user that finishes the race and not before, you should add a check in the registered function to see if plr equals to the local player.

something like;

function registered(plr,vehicleP,timeP) 
    if ( plr == getLocalPlayer() ) then 
        if vehicleP and timeP then 
            local text = getPlayerName(plr).."-"..getVehicleNameFromModel(vehicleP).." time: "..string.format("%.2f",timeP).."\n" 
            if text then 
                table.insert(drag,text) 
                addText() 
            end 
        end 
        if #drag > 5 then 
            table.remove(drag, 1) 
        end 
    end 
end 
addEvent("registerPredkosc",true) 
addEventHandler("registerPredkosc",resourceRoot,registered) 

Link to comment

This function is triggered from c-side

  
addEventHandler("onClientColShapeLeave", colMeta, function(el,md) 
    if getElementType(el)~="vehicle" then return end 
    local veh=getPedOccupiedVehicle(localPlayer) 
    if veh and veh==el  and getVehicleController(veh)==localPlayer then 
    local vehicleP = getElementModel(veh) 
     
        if ts then 
                    local czas=getTickCount()-ts 
                    local tczas=string.format("%.3fs", czas/1000) 
                    outputChatBox("Twój czas:".."\n"..tczas) 
                    local timeP = czas/1000 
                    triggerServerEvent("registerPredkosc", resourceRoot, localPlayer, vehicleP, timeP) 
                end 
        triggerEvent("startStoper", root, false) 
        ts=nil 
    end 
end) 
  
  

I think, must add something like refresh the text before createElement('text')

Link to comment

By looking at that, you're triggereing the event right here;

triggerServerEvent("registerPredkosc", resourceRoot, localPlayer, vehicleP, timeP) 

Something that conserns me though is the fact that you're sending it to the server. A thing you can do is try to see if the text is already in the table by looping over it;

function registered(plr,vehicleP,timeP) 
    if vehicleP and timeP then 
        local text = getPlayerName(plr).."-"..getVehicleNameFromModel(vehicleP).." time: "..string.format("%.2f",timeP).."\n"; 
        if text then 
            addText() 
             
            for i = 1, #drag do 
                if ( drag [ i ] == text ) then 
                    return; 
                end 
            end 
  
            table.insert(drag,text) 
        end 
    end 
    if #drag > 5 then 
        table.remove(drag, 1) 
    end 
end 
addEvent("registerPredkosc",true) 
addEventHandler("registerPredkosc",resourceRoot,registered) 

Link to comment

That's weird, the table isn't filled in with double values like that, so the drawing must be wrong. Let's see what's going on in there..

edit; the drawing looks normal too. I'm seriously thinking that you're adding all the elements over and over again without clearing the table somewhere. I would need both of the files, entirely, to see where it's going wrong. You could send these in PMs if you don't want to put them out in the open. :lol:

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