alright i have a quick question. I have this code in a client side script, It essentially is a timer that when it finishes gives the player an ak.
The question is, if i were to execute the start command, would the timer start for everyone in the server or just me? Would the /start command need to trigger a server event? Thanks
function giveweapon(weaponHash)
GiveWeaponToPed(GetPlayerPed(-1), GetHashKey(weaponHash), 999, false, false)
end
RegisterCommand('start', function()
local time = 10
while (time ~= 0) do
Wait( 1000 )
time = time - 1
print(time+1)
end
TriggerEvent('startgame')
end)
AddEventHandler('startgame', function()
while true do
DisableControlAction(0, 37, true);
Citizen.Wait(1)
giveweapon("weapon_assaultrifle")
SetCurrentPedWeapon(GetPlayerPed(-1), GetHashKey("weapon_assaultrifle"), true)
end
end)
Like something along the lines of this?
Client Script
function giveweapon(weaponHash)
GiveWeaponToPed(GetPlayerPed(-1), GetHashKey(weaponHash), 999, false, false)
end
RegisterCommand('start', function()
TriggerServerEvent('serverstart')
end)
AddEventHandler('startgame', function()
while true do
DisableControlAction(0, 37, true);
Citizen.Wait(1)
giveweapon("weapon_assaultrifle")
SetCurrentPedWeapon(GetPlayerPed(-1), GetHashKey("weapon_assaultrifle"), true)
end
end)
Server Script
AddEventHandler('serverstart', function()
print('hello')
local time = 10
while (time ~= 0) do
Wait( 1000 )
time = time - 1
end
TriggerClientEvent('startgame')
end)
RegisterCommand('start', function()
TriggerEvent('serverstart')
end)
function giveweapon(weaponHash)
GiveWeaponToPed(GetPlayerPed(-1), GetHashKey(weaponHash), 999, false, false)
end
AddEventHandler('startgame', function()
while true do
DisableControlAction(0, 37, true);
Citizen.Wait(1)
giveweapon("weapon_assaultrifle")
SetCurrentPedWeapon(GetPlayerPed(-1), GetHashKey("weapon_assaultrifle"), true)
end
end)
server.lua
AddEventHandler('serverstart',function()
local time = 2
while (time ~= 0) do
Wait( 1000 )
time = time - 1
print(time+1)
end
TriggerClientEvent('startgame', -1)
end)
Why not create the command on the server side to trigger an event to all clients? That way you can implement some permissions as well so that everyone cannot just run the command. Also, why are you making a while statement on the client side? Just have it disable the key, give the weapon and set the peds weapon. The while statement is not needed. And finally, whats with the weird print you have on the server side? Not necessary either.
I put the while statement in there so that if you die you get given an ak instantly. It may be clunky and theres probably a better way to do it, but it works!
with putting a command on server side, i dont know how that works? Could i just put in RegisterCommand? If its not too much to ask could i get an example?
RegisterCommand("startgame",function()
local player = source
-- Do permission check if necessary
TriggerClientEvent("startnewgame", -1, true)
end)
RegisterCommand("stopgame",function()
local player = source
-- Do permission check if necessary
TriggerClientEvent("startnewgame", -1, false)
end)
Clientside:
local isGameStarted = false
RegisterNetEvent("startnewgame")
AddEventHandler("startnewgame",function(gameState)
if gameState ~= nil then
isGameStarted = gameState
GiveWeaponToPed(PlayerPedId(),`weapon_assaultrifle`,999,false,true)
end
end)
Citizen.CreateThread(function()
while true do
if isGameStarted then
DisableControlAction(0, 37, true)
end
Wait(0)
end)
AddEventHandler("playerSpawned",function()
if isGameStarted then
GiveWeaponToPed(PlayerPedId(),`weapon_assaultrifle`,999,false,true)
end
end)
also i got it working with the command client side. Just needed to add RegisterNetEvent in client and RegisterServerEvent in server. But i am still curious about the server sided command.
alright, thank you so much.
One final quick question, can you explain how it checks for permissions here? and also why do you put true as the last parameter in TriggerClientCommand but on the stopgame triggerclientevent its false
. Just curious
Checking for permissions will depend on your framework.
As for the true/false question, startgame, tells the clients that the game is started, which will then disable their tab key, give them the assaultrifle, and put it in their hands. It also tells the client, if they die to only give the assaultrifle on respawn if the gamemode has been started. But if the gamemode is disabled, it will not disable the tab key, and it will not give them a weapon on respawn.