[HELP] help with a part of the script

Well it’s important to clarify that my English is not very good so I will try to explain myself in the best way

I have a function that gives 3 results according to the probability of dropping an item. What I do not want to do is call 3 serverevent to give those items. And I would not be coming out doing it optimal

Cient

function dropitem ()
    possibility = math.random(1, 100)
    if (possibility > 10) and (possibility < 90) then
        dropcplata = math.random(5, 25)
        TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"find iron : "..dropcplata.."")
        --TriggerServerEvent("give:item", iron)
    else
        TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"you did not find silver ")
    end
    Wait(2500)
    possibility = math.random(1, 100)
    if (possibility > 30) and (possibility < 80) then
        dropbgold = math.random(3, 10)
        TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"find gold : "..dropbgold.."")
       --TriggerServerEvent("give:item", gold)
    else
        TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"you did not find silver")
    end
    Wait(2500)
    possibility = math.random(1, 100)
    if (possibility > 40) and (possibility < 55) then
        dropdiamon = math.random(1, 3)
        TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"find diamon : "..dropdiamon.."")
        --TriggerServerEvent("give:item", diamon)
    else
        TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"you did not find silver")
    end
end

I could really call an event for everyone, but I would like to know how to do it best.

That’s why I do not put the serv.lua

thank you very much: D :heart:

You could make a function:

function getPossibility(param1, param2, param3, param4)
  local possibility = math.random(param1, param2)
  if possibility > param3 and possibility < param4 then
    return true
  else
    return false
  end
end

Do all the randoms and “possibility” in the server side ? And use Settimeout instead of Wait().
Instead of calling 3 server event, just call 1, that way you are doing all in 1!

Client:

TriggerServerEvent("yo")

Server:

local PlayersYo = {}

function dropitem(source)
    SetTimeout(2500, function()
	    if PlayersYo[source] == true then
            possibility = math.random(1, 100)
            if (possibility > 10) and (possibility < 90) then
                dropcplata = math.random(5, 25)
                TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"find iron : "..dropcplata.."")
            else
                TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"you did not find silver ")
            end
		end
	end)
	SetTimeout(5000, function()
	    if PlayersYo[source] == true then
            possibility = math.random(1, 100)
            if (possibility > 30) and (possibility < 80) then
                dropbgold = math.random(3, 10)
                TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"find gold : "..dropbgold.."")
            else
                TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"you did not find silver")
            end
		end
	end)
    SetTimeout(7500, function()
	    if PlayersYo[source] == true then
            possibility = math.random(1, 100)
            if (possibility > 40) and (possibility < 55) then
                dropdiamon = math.random(1, 3)
                TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"find diamon : "..dropdiamon.."")
            else
                TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"you did not find silver")
			end
        end
	end)
end

RegisterServerEvent('yo')
AddEventHandler('yo', function()

	local _source = source
	PlayersYo[_source] = true
	dropitem(_source)

end)

I may have explained it wrong, the random and the possibility I do everything in client, and from there I call the server

The theme is a drop item, and the idea is to appear in the chat

find iron: 5
dont find gold
find diamon: 1

and that amount can be used on the server, or does that have to be all on the server?

thanks for your help

It would be running the server function, and when I call for the items I get 0 of the 3 types that I put

1 Like

The way I found this to work was by doing the random per client and for each item to call a server event, although it can surely be done more optimally.
I leave it to you as it worked for me if someone can help me optimize it. Thanks

I put part of the code, since I look for help in this part xD
client.lua

possibility = math.random (1, 100)
	if (possibility> 10) and (possibility <90) then
		dropcplata = math.random (5, 25)
			TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "you found silver:" ..dropcplata .. "")
			TriggerServerEvent("treasure:doysilver", dropcplata)
	else
		TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "You did not find silver")
	end
Wait (1500)
possibility = math.random (1, 100)
	if (possibility> 30) and (possibility <80) then
		dropbgold = math.random (3, 10)
		TriggerEvent ('chatMessage', 'SYSTEM', {255, 0, 0}, "you found gold:" ..dropbgold .. "")
		TriggerServerEvent("treasure:doygold", dropbgold)
	else
		TriggerEvent ('chatMessage', 'SYSTEM', {255, 0, 0}, "You did not find gold")
	end
Wait (1500)
possibility = math.random (1, 100)
	if (possibility> 40) and (possibility <55) then
		dropdiamon = math.random (1, 3)
		TriggerEvent ('chatMessage', 'SYSTEM', {255, 0, 0}, "you found damiante:" ..dropdiamon .. "")
		TriggerServerEvent ("treasure:doydiamon", dropdiamon)
	else
		TriggerEvent ('chatMessage', 'SYSTEM', {255, 0, 0}, "You did not find a diamond")
	end
Wait (1500)
possibility = math.random (1, 100)
	if (possibility> = 45) and (possibility <= 47) then
		dropesmeral = math.random (1, 1)
		TriggerEvent ('chatMessage', 'SYSTEM', {255, 0, 0}, "What the fuck did you find" ... dropesmeral .. "of emerald")
		TriggerServerEvent ("treasure: doyesmeral", dropesmeral)
	else
		TriggerEvent ('chatMessage', 'SYSTEM', {255, 0, 0}, "You did not find esmeralnda")
	end

server.lua

RegisterServerEvent('tesoro:doysilver')
AddEventHandler('tesoro:doysilver',function(mount)
	TriggerEvent('es:getPlayerFromId', source, function(user)
	local xPlayer = ESX.GetPlayerFromId(source)
        total = mount
		xPlayer.addInventoryItem('silver_necklace', total)
    end)
end)

etc..

THX :heart: