Hello everyone,
I need help for making a script.
I want to create a script that will allow me to give a function to a prop.
To give an example, I would like to put on sale in a shop a bang, when I use this bang he put a prop on the ground, until there i know how do it.
And i want when a player distance below X distance he can use it and when he used and when he uses it, it makes him feel like he’s been smoking drugs.
Then if was possible he can use it X times after props is delete.
Easiest way to accomplish this, is whenever you’re spawning that object for use … keep a reference to it’s ID in a table and then just check if items of that type is nearby and then check if you spawned it for that use.
I’m doing this with drops for my inventory system.
Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)
local pedCoord = GetEntityCoords(GetPlayerPed(-1))
if DoesObjectOfTypeExistAtCoords(pedCoord["x"], pedCoord["y"], pedCoord["z"], 2.0, GetHashKey("p_michael_backpack_s"), 0) then
local Bag = GetClosestObjectOfType(pedCoord["x"], pedCoord["y"], pedCoord["z"], 2.0, GetHashKey("p_michael_backpack_s"), false, false, false)
if dropList[Bag] ~= nil then
bagId = Bag
else
bagId = nil
end
else
bagId = nil
end
end
end)
Every 1000 frames I’m checking if there is any objects of type p_michael_backpack_s within a 2.0 radius of the player. If there is I am then grabbing its object id and checking if that object id is stored in a list that I’m adding to when I actually spawn the object. If an object exists and its one I spawned for the sake of this resource, I’m storing it in a variable that I’m checking for in another thread.
Due to me printing text in the 3DWorld I have to run this every frame and if you do that while checking if the object exists and such … it’ll tank performance so i’d suggest just not doing that.
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if bagId ~= nil then
local bagCoords = GetEntityCoords(bagId)
exports['mythic_base']:Print3DText(bagCoords, '~y~[E] ~s~Open Backpack')
if IsControlJustReleased(0, 54) then
-- Do stuff if they're near the object & press E
end
end
end
end)
Yeah, this is possible. Would require to keep reference on the server-side and verify that it can be used with a server call before doing it. But that’s honestly fairly simple (When they use item > Call server event > Server event will call the client function to apply affects if there are uses left).
Technically, you could do this without making a server-call and just sending packets to clients when objects are used to update use count and then check it on the client. But that’s open for abuse for idiot script kiddies so wouldn’t say it’s a good idea.
As for spawning the object, honestly I just found it far easier to spawn objects for each client than trying to figure out networking the objects and I just sync drops every hour between connected clients. Chances are this is the wrong way to do it, but it works and I’ve tested several clients connected and works as I want so eh’
Video Example
Peformance wise, this way has been great. The only jump in resource usage I get is from printing 3D Text.
Hey, after 9 month i’m coming back. Thank you very much again all works fine but i need to do more. I work on my nightclub job and i want use different object for different drink but when i try to use different object and when 1 of his object are not in radius that’s not work. I try to increase radius to 1000 or over but that’s not work and i think it’s not best solution.
local objlist = {}
RegisterNetEvent('sp_object:SpawnProp')
AddEventHandler('sp_object:SpawnProp', function(prop)
...
...
obj = Createobject(GetHashKey(prop), coords["x"], coords["y"], coords["z"], 1, 1, 0)
PlaceObjectOnGroundProperly(obj)
table.insert(objlist, {objc = obj, model = prop})
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)
if next(objlist) ~= nil then
for k,v in pairs(objlist) do
local pedCoord = GetEntityCoords(GetPlayerPed(-1))
if DoesObjectOfTypeExistAtCoords(pedCoord["x"], pedCoord["y"], pedCoord["z"], 2.0, GetHashKey(v.model), 0) then
local Obj = GetClosestObjectOfType(pedCoord["x"], pedCoord["y"], pedCoord["z"], 2.0, GetHashKey(v.model), false, false, false)
if Obj == v.objc then
objId = Obj
objPr = v.model
else
objId = nil
end
else
objId = nil
end
end
else
objId = nil
end
end
end)