[Help] Lua table return nil value

Hi there, I am newer to coding and only been doing it for a couple of months. I am currently working on a emote script that uses props and flags for upper body only. Now this is what I’ve come up with.

This is my table for right now.

local emote = {
["clipboard"] = {
    animDict = "move_m@clipboard", 
    animName = "idle", 
    speed = 8, 
    speedX = -8, 
    duration = -1, 
    flags = 49
    },
["box"] = {
    animDict = "anim@heists@box_carry@", 
    animName = "idle", 
    speed = 8, 
    speedX = -8, 
    duration = -1, 
    flags = 49,
    },
}

I haven’t began to add props since I cannot seem to get anything but a nil value when I run this function.

function playEmote(animDict, animName, speed, speedX, duration, flags)
    if not DoesEntityExist(GetPlayerPed(-1)) then
        return false
    end

    loadAnimDict(emote[name][animDict])
    TaskPlayAnim(GetPlayerPed(-1), animDict, animName, speed, speedX, duration, flags, 0, 0, 0, 0)
    return true
    
end

This is how I am running the function

        if playEmote(emote[name][animDict], emote[name][animName], emote[name][speed], emote[name][speedX], emote[name][duration], emote[name][flags]) then
        end

I know I am missing something, maybe a “for loop” that returns all the index in the first table as name? I’ve tried googling it but cannot seem to find the answer. Any help would be awesome!

shouldn’t it be loadAnimDict(emote[animName][animDict])?

the loadAnimDict is another function I have

function loadAnimDict(dict)
	while (not HasAnimDictLoaded(dict)) do
		RequestAnimDict(dict)
		Citizen.Wait(5)
	end
end

and the native only requests the animDictionary which is all it needs

loadAnimDict(emote[name].animDict)

I mean your playEmote() function recieves the parameters animDict and animName. I don’t see where the parameter name comes from.
And if you wanted to just address the animDict field in your table you would go like this: loadAnimDict(emote[name]["animDict"])

Edit: or like xander wrote with a dot

both suggestions still seem to return a nil value

Its because it’s a different value than animDict. When he passes an argument the code is looking for this

loadAnimDict(emote[name][“random_string_for@dicts”])

Which doest exist.

If he uses my way above it will work.

Show me how you are triggering that play emote method

Still whats name?

AddEventHandler('emote:invoke', function(name)
    local player = PlayerPedId()

    local pedHoldingWeapon = IsPedArmed(GetPlayerPed(-1), 7)
    if pedHoldingWeapon then
        SetCurrentPedWeapon(GetPlayerPed(-1), 0xA2719263, true)
    end

    if IsPedInAnyVehicle(GetPlayerPed(-1)) then -- Returns if the player is in any vehicle
        return false
    end

 if (emote[name] ~= nil) then

        if playEmote(emote[name].animDict, emote[name].animName, emote[name].speed, emote[name].speedX, emote[name].duration, emote[name].flags) then
        end
    else
        TriggerEvent("chatMessage", "Error", {255,0,0}, "Invalid emote name") -- Saying if the name wasn't in the dictionary
    end
end)

Wait. Why not just pass name to the method and let the method recieve all the table data.

The way you are doing it. It seems you can’t get the data in there because you don’t have the name string index in that method.

Could you elaborate? How would I go about doing so?

function playEmote(index)
    if not DoesEntityExist(GetPlayerPed(-1)) then
        return false
    end

    loadAnimDict(emote[index].animDict)
    TaskPlayAnim(GetPlayerPed(-1), emote[index].animDict, emote[index].animName, emote[index].speed, emote[index].speedX, emote[index].duration, emote[index].flags, 0, 0, 0, 0)
    return true
    
end
if playEmote(name) then

end

So that doesn’t return a nil value but it also doesn’t play the animation.

however

if playEmote(emote[name]) then

end

does return a nil value

ps. thanks a bunch for the assistance!

Well I am not the best at help rn since I am on my phone. If the animation isn’t playing my guess is that it’s not letting the anim dictionary load before calling it.

no worries, appreciate the help regardless!!

So i found my issue…it was for some reason not playing the animation while not having a decimal in the speed parameter for the TaskPlayAnim native… my initial code worked fine. well except

 loadAnimDict(animDict)

in the playAnim function.

now to add props…

thanks for the help @xander1998 …looking forward to your K9 script :smile:, thanks a bunch as well @Greenlander

1 Like