Need helping with my script


this is my code the problem with it is that whenever i get the notification to press e to accept the call it doesn’t trigger the event that is supposed to be triggered after e is pressed please help me

It isn’t in loop.

hey,

The problem with your code, is that it isn’t in a loop, when you create a client sided thread, that thread will execute on the player join. The thing is, you are only executing the code once, so when you are looking for keyboard inputs with IsControlJustReleased(0, 38) you are checking it once, when the player joined.

You need to permanently listen for the user inputs to detect it and trigger an action accordingly,
this should work (I haven’t tested it):

local QBCore = exports['qb-core']:GetCoreObject()
local gettingCall = false

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() -- Event registering should be located outside threads
    Wait(20000)
    QBCore.Functions.Notify("You Are Getting A Phone Call Press E To Accept It And Z To Decline It.", "error")
    gettingCall = true
end)

Citizen.CreateThread(function()
    while true do               -- infinite loop that will loop while the player is in game
        Citizen.Wait(1)
        if (IsControlJustReleased(0, 38) and gettingCall) then
            TriggerServerEvent('InteractSound_SV:PlayOnSource', 'cellcall', 0.20)
            TriggerServerEvent('InteractSound_SV:PlayOnSource', 'cellcall', 0.20)
            ExecuteCommand("e phonecall")
            QBCore.Functions.notify("Welcome To the City!", "success")
            ExecuteCommand("e c")
            gettingCall = false
        elseif (IsControlJustReleased(0, 20) and gettingCall) then
            QBCore.Functions.Notify("You Declined The Call", "success")
            gettingCall = false
        end
    end
end)
1 Like

hey thanks for helping me out it’s working now and i was able to add some more code and finish my project but currently i am trying to add it so that it has a random chance if you would receive the call or not but i can’t seem to use math.random properly in this code if you could help me out i would really appreciate it

Hey,

No worries !

About random numbers, never actually got to use that feature in lua, but to my understanding and after reading this documentation: lua-users wiki: Math Library Tutorial , you need to initialize a seed for your random generator so that it doesn’t generate the same sequence of numbers when re-initialized.

To initialize your seed, a good bet would be: math.randomseed( os.time() )

What that does is initialize your seed to the current time, which avoids the number sequence being repeated when you restart your server. I recommend running this bit of code once on the player join, so in a Citizen Thread.

you then can retrieve a random value using math.random() anywhere in your code.

I really recommend you read the documentation on lua randomizers, it will help you get a better understanding of the randomness of this technique

1 Like

hey thanks for the answer i did read it but i tried but i failed if you could help me out a bit?

Can’t really provide any more help on this, the only times I’ve used random numbers were in C, Python & JavaScript, never used it in lua, try to understand the documentation, sadly I can’t help you any further

1 Like

well i understand the documentation the problem is i cant really seem to use the functions properly in my code but i will try again and anyways thanks i really appreciate that you helped me out with this mini project

You have to make sure you set the seed at the launch of your program with the math.randomseed( os.time() )

then in your code, if you for example, want to retrieve a random integer between 1 and 100, you can call: math.random(1, 100), you can specify limits to your random number as indicated in the documentation.

Good luck with your project !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.