Hero192 Posted February 24, 2016 Share Posted February 24, 2016 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
Bonus Posted February 24, 2016 Share Posted February 24, 2016 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 3. The distance is in 3D. If you fly high, you got a distance too. Link to comment
Hero192 Posted February 24, 2016 Author Share Posted February 24, 2016 No, Client doesn't work in client side, just defined it with localPlayer, well I will try what you told me, btw why this code cause lag? Link to comment
Bonus Posted February 24, 2016 Share Posted February 24, 2016 This code shouldn't cause lag. Link to comment
Hero192 Posted February 24, 2016 Author Share Posted February 24, 2016 This code shouldn't cause lag. Alright, Is there's a way to check how many meters left to complet 200meters? if yes please tell me the way Link to comment
Bonus Posted February 24, 2016 Share Posted February 24, 2016 getDistanceBetweenPoints3D or 2D Link to comment
Hero192 Posted February 24, 2016 Author Share Posted February 24, 2016 Well so I have a last question, how to get shop's position for each ped aimed on. For example: If I aim on the ped who exist in shop A, the script will get distance of shop A..etc Just give me a way to follow please Link to comment
Hero192 Posted February 25, 2016 Author Share Posted February 25, 2016 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
Bonus Posted February 25, 2016 Share Posted February 25, 2016 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
Hero192 Posted February 25, 2016 Author Share Posted February 25, 2016 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
Bonus Posted February 25, 2016 Share Posted February 25, 2016 You have to loop the table with pairs. Link to comment
Hero192 Posted February 25, 2016 Author Share Posted February 25, 2016 You have to loop the table with pairs. How, I don't know how to deal with this kind of table and thanks !! alot bonus Link to comment
Bonus Posted February 25, 2016 Share Posted February 25, 2016 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
Hero192 Posted February 25, 2016 Author Share Posted February 25, 2016 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
Bonus Posted February 25, 2016 Share Posted February 25, 2016 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. Link to comment
Hero192 Posted February 25, 2016 Author Share Posted February 25, 2016 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
Bonus Posted February 25, 2016 Share Posted February 25, 2016 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
Hero192 Posted February 25, 2016 Author Share Posted February 25, 2016 Working fine, thanks alot !! man for your time and your hard work I really appreciate that 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