Jump to content

I still need help please


Hero192

Recommended Posts

Hello guys, I have this part of code suggested by tosfera, but the problem is , when the player leaves the store and become in interior 0, it causes lag.

and my main problem is, I want when the player leaves the store he needs to move away of store about 200meters, then a message will output.

BUT, sometime it outputs this message and give player's money even he didn't pass the distance necessary.

I hope you guys can give me a hand, this is so annoying, I tried all my ways but with no result.

function renderDistanceCheck () 
    if (isPedDead(client) or getElementInterior(client) > 0) then return end 
    if ((not isPedDead(client)) and getElementInterior(client) == 0) then 
        local px,py,pz = 1199.13, -918.07, 43.12  
        local x,y,z = getElementPosition(client) 
        local dist = getDistanceBetweenPoints3D(px, py, pz, x, y, z)  
        if (dist >= 200) then  
            outputChatBox("Succesfully you passed 200meters away of shop") 
            removeEventHandler ("onClientRender", root, renderDistanceCheck) 
        end 
    end 
end 

Link to comment

1. The 2. line isn't needed, delete it.

2. You use client? Why?

It's clientsided, why not use localPlayer?

Does it even work with client?

Maybe its working and I never knew that client == localPlayer o.O

3. The distance is in 3D.

If you fly high, you got a distance too.

Link to comment

I already using this function, I just want to add my script in more shops but to do that, I need a way to define each ped that got aimed to be able to get shop position.

Like if I aim on ped in shop ls the player will return the cordination of shop ls, because I want to make a distance system for each shop.

Like need to walk 200 meters far of shop to get paid.

Well i think I had explained it well please anyone can show me the right way?

Link to comment

You shouldn't get the position of the ped, because the ped is in another interior and other position.

If the player goes out of the interior he could be somewhere far away from the ped position.

There are 2 easy ways:

1. Save the peds in a table with the coordinates of the shop in interior 0.

Like that:

local shoppeds = {  
   [createPed ( ... )] = { 123, 1512, 10.2 }, 
   {createPed ( ... )] = { 556, -234, 5.6 }, 
    .... 
} 

Then you can get the position with:

local x, y, z = shoppeds[ped][1], shoppeds[ped][2], shoppeds[ped][3] 

You are using onClientPlayerTarget for your shoprob, right?

So you get the ped in that function, no need for getPedTarget

2. Not sure if it will work:

When the shoprob starts, create an addEventHandler ( "onClientMarkerLeave", root, asdasda ).

There you can ask, if getElementInterior == 0.

If its true, you can remove this event handler and get the position of source.

source has to be the marker in interior 0 to get into the shop, the entrance for the shop.

But care:

You have to remove the onClientMarkerLeave Event when the player dies or the shoprobs stops.

Link to comment

Why this won't be loaded?

local peds = { 
    [createPed(205, 376.51, -65.84, 1001.50, 180)] = {1199.13, -918.07, 43.12, 1, 10},   
    [createPed(205, 376.51, -65.84, 1001.50, 180)] = {-1912.27, 828.02, 35.21, 2, 10},   
    [createPed(205, 376.51, -65.84, 1001.50, 180)] = {-2336.94, -166.64, 35.55, 3, 10},   
} 
  
addEventHandler("onClientResourceStart", resourceRoot,  
function () 
    if peds[ped] then  
        setElementFrozen(peds[ped], true) 
        setElementInterior(peds[ped], peds[ped][5]) 
        setElementDimension(peds[ped], peds[ped][4]) 
    end  
end) 

Link to comment
local peds = { 
    [createPed(205, 376.51, -65.84, 1001.50, 180)] = {1199.13, -918.07, 43.12, 1, 10},   
    [createPed(205, 376.51, -65.84, 1001.50, 180)] = {-1912.27, 828.02, 35.21, 2, 10},   
    [createPed(205, 376.51, -65.84, 1001.50, 180)] = {-2336.94, -166.64, 35.55, 3, 10},   
} 
  
for ped, array in pairs ( peds ) do 
     setElementFrozen ( ped, true ) 
     setElementInterior ( ped, array[5] ) 
     setElementDimension ( ped, array[4] ) 
end 

Dont need to put it into onClientResourceStart or any other function.

Link to comment

The loop worked thanks but I still facing my main problem,

It gives the cash dirrectly on player leave the store, I don't know if I did something wrong in the loop

function renderDistanceCheck () 
    if ((not isPedDead(client)) and getElementInterior(client) == 0) then 
        for ped, v in pairs (peds) do 
            local ox, oy, oz = v[1], v[2], v[3] 
            local x, y, z = getElementPosition(client) 
            local dist = getDistanceBetweenPoints2D(ox, oy, x, y)  
            if (dist >= 300) then  
                triggerServerEvent("robstore:giveReward", client, client) 
                addEventHandler("onClientRender", root, render) 
                removeEventHandler ("onClientRender", root, renderDistanceCheck) 
                setTimer(function() removeEventHandler("onClientRender", root, render) end, 5000,1) 
            end  
        end 
    end 
end 

I hope you can solve this and thanks again @Bonus for the hard work

Link to comment
Yes, thats not the way I meant.

You are using onClientPlayerTarget on the ped, right?

Just save the ped there and use it in renderDistanceCheck.

I tried something like that, but not working, can you correct me please , thanks alot

function targetPed(target) 
    for ped ,v in pairs(peds) do  
        if (ped == target) then  
            return true 
        end 
    end 
end 
  
addEventHandler("onClientPlayerTarget", client,   
function (target) 
    if (target) and (getElementType(target) == "ped") and storePed(target) and (source == client) then  
        if isTimer(cooldown) then return end 
        triggerServerEvent("onRob", client) 
        setPedAnimation(target, "shop", "SHP_Rob_HandsUp", -1, false) 
        cooldown = setTimer(function() end, 300000, 1) 
    end 
end) 

Link to comment

The function targetPed is really useless.

You are using storePed, what is that?

local shoprobped = nil 
  
addEventHandler("onClientPlayerTarget", client,   
function (target) 
    if isElement ( target ) and peds[target] then 
        if isTimer(cooldown) then return end 
        triggerServerEvent("onRob", client) 
        setPedAnimation(target, "shop", "SHP_Rob_HandsUp", -1, false) 
        cooldown = setTimer(function() end, 300000, 1) 
        shoprobped = target       -- save the ped 
    end 
end) 
  
... 
  
function renderDistanceCheck ()  
    if ((not isPedDead(client)) and getElementInterior(client) == 0) and isElement ( shoprobped ) then 
            local ox, oy, oz = peds[shoprobped][1], peds[shoprobped][2], peds[shoprobped][3]       -- use the ped 
            local x, y, z = getElementPosition(client) 
            if (getDistanceBetweenPoints2D(ox, oy, x, y) >= 300) then 
                triggerServerEvent("robstore:giveReward", client, client) 
                addEventHandler("onClientRender", root, render)   -- I hope render is "local" here 
                removeEventHandler ("onClientRender", root, renderDistanceCheck) 
                setTimer(function() removeEventHandler("onClientRender", root, render) end, 5000,1) 
            end 
        end 
    end 
end 

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