Heya,
I don’t usually like asking for help as I’m always worried it’ll be something really stupid so I usually prefer to figure it out by myself first, but I just cannot seem to fix this.
Long story short, I’m making a /use command for people tu use items in their inventory, this command should prompt a message in chat, perform a custom action and play an animation.
I set up my own animation handler as I find “TaskPlayAnim” to be a bit fiddly and I wanted to be able to call animations client side. I set up the whole thing and it worked fine until I got a second player on to test it, and that’s when I realised that I’d been calling the player as “GetPlayerPed(-1)” throughout the whole script, so naturally the animation played for everyone even if only one person used /use.
So I tried to fix this, and I thought it’d be an easy fix.
Yet I can’t seem to GetPlayerPed() to not return 0 whatever I put into it.
I’ve stored the playerid in a variable that I called “player” and that works, it returns 2 same as what I have in the in-game player list for me. But when I input it into “GetPlayerPed(player)” it just returns 0…
usecmd.lua (server script)
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
print("esx:getSharedObject ran successfully, ESX")
-- USE COMMAND (/use item_name) Outcome: Play appropriate animation, show message and apply effects (if any)!
TriggerEvent('es:addCommand', 'use', function(source, args, user)
if args[1] ~= nil then -- Make sure that there is a word after "/use"
local itemname = args[1];
local pname = GetPlayerName(source);
local pedid = GetPlayerPed(source);
local xPlayer = ESX.GetPlayerFromId(source);
local sourceItem = xPlayer.getInventoryItem(itemname);
print("Received command request '/use'! Set pname to: " .. pname .. " and 'pedid' to " .. pedid);
if (sourceItem.count - 1) >= 0 then -- Make sure the player has enough of the item to use
xPlayer.removeInventoryItem(itemname, 1)
-- BEER ITEM --
if itemname == 'beer' then
local dict = "amb@code_human_wander_drinking@beer@male@idle_a";
local name = "idle_a";
local prop = "prop_amb_beer_bottle";
local propX = 0.145;
local propY = 0.07;
local propZ = -0.055;
local propRotX = -80.0;
local propRotY = 0.0;
local propRotZ = 0.0;
local shouldLoop = true;
local duration = -1;
local dropOnEnd = false;
TriggerClientEvent("sendProximityMessageMe", -1, source, pname, "drank a beer.")
TriggerClientEvent("BRPC:PlayAnim", -1, source, pedid, dict, name, prop, propX, propY, propZ, propRotX, propRotY, propRotZ, shouldLoop, duration, dropOnEnd)
TriggerClientEvent('Radiant_Drunk:GetDrunk', -1, 2) -- I edited the Radiant_Drunk script to have a BAC level and a /blow command that returns said BAC.
end
else
TriggerClientEvent('BRPC:NoItemError', -1)
end
else
TriggerClientEvent('BRPC:InvalidItemName', -1)
end
end)
animhandler.lua (client script)
ESX = nil;
local player = nil;
local ped = nil;
local hasAnimated = false;
local isAnimating = false;
local shouldLoop = false;
local animDict = nil;
local animName = nil;
local heldProp = nil;
local previousProp = nil;
local animFlag = 50;
local duration = -1;
Citizen.CreateThread(function()
while true do
Citizen.Wait(0);
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end);
end
Citizen.Wait(0);
if isAnimating then
if ped ~= nil then
SetPedCanHeadIk(ped, false);
if not hasAnimated then
RequestAnimDict(animDict)
while(not HasAnimDictLoaded(animDict)) do Citizen.Wait(0) end
Citizen.Wait(0);
TaskPlayAnim(ped, animDict, animName, 2.0, 2.0, duration, animFlag, 1, false, false, false);
print("Animating...");
print("Animation Dictionary: " .. animDict);
print("Animation Name: " .. animName);
print("Animating for: " .. ped);
hasAnimated = true;
end
end
end
Citizen.Wait(0);
if IsControlPressed(0, 25) or IsControlPressed(0, 24) or IsControlPressed(0, 140) or IsControlPressed(0, 141) or IsControlPressed(0, 142) or IsControlPressed(0, 263) or IsControlPressed(0, 264) or IsControlPressed(0, 21) or IsControlPressed(0, 16) or IsControlPressed(0, 17) or IsControlPressed(0, 37) then -- If any of the attack inputs, aiming or the sprint key are pressed
if isAnimating == true then
print("Trying to aim a weapon, clearing animation...");
ClearPedTasksImmediately(ped);
isAnimating = false;
hasAnimated = false;
animDict = nil;
animName = nil;
SetPedCanHeadIk(ped, true);
TriggerEvent('BRPC:AnimDropProp', -1);
end
end
end
end)
RegisterNetEvent("BRPC:PlayAnim")
AddEventHandler("BRPC:PlayAnim", function(id, pedid, dict, name, prop, propX, propY, propZ, propRotX, propRotY, propRotZ, loop, dur, dropOnEnd)
print("Received call for BRPC:PlayAnim event!");
isAnimating = false; -- Reset isAnimating to false
-- Get/Set the variables
animDict = dict;
animName = name;
shouldLoop = loop;
animRate = loopSpd;
duration = dur;
player = id;
ped = pedid;
print("Set 'player' variable to: " .. player);
print("Set 'ped' variable to: " .. ped);
ClearPedTasksImmediately(ped); -- Clear any prior animations or actions
if shouldLoop then
animFlag = 51;
else
animFlag = 50;
end
if animDict ~= nil and animName ~= nil then
isAnimating = true;
hasAnimated = false;
print("Set 'isAnimating' to 'true' and 'hasAnimated' to 'false'!");
end
if prop ~= nil then
TriggerEvent('BRPC:AnimDropProp', -1);
heldProp = CreateObject(GetHashKey(prop), 0, 0, 0 ,true, true, true);
AttachEntityToEntity(heldProp, ped, GetPedBoneIndex(ped, 57005), propX, propY, propZ, propRotX, propRotY, propRotZ, true, true, false, true, 1, true);
end
if duration ~= -1 and dropOnEnd then
Citizen.Wait(duration);
TriggerEvent('BRPC:AnimDropProp', -1);
end
end)
-- Obsolete
RegisterNetEvent("BRPC:AnimLoopTimer")
AddEventHandler("BRPC:AnimLoopTimer", function()
print("Received call for BRPC:AnimLoopTimer event!");
if hasAnimated then
Citizen.Wait(animRate);
hasAnimated = false;
end
end)
-- End of Obsolete
-- Drops the spawned prop
RegisterNetEvent("BRPC:AnimDropProp")
AddEventHandler("BRPC:AnimDropProp", function()
print("Received call for BRPC:AnimDropProp event!");
if heldProp ~= nil then
previousProp = heldProp
DetachEntity(heldProp, false, false)
Citizen.Wait(500)
DeleteEntity(heldProp)
heldProp = nil
end
if previousProp ~= nil then
Citizen.Wait(0)
DeleteEntity(previousProp)
end
end)

)