Need help creating a ped for script im attempting to create for my first time

Trying to create a script where i walk up to a ped and buy a certain item off of him new to deving btw and i got the code straight fivem natives i just cant figure out how to close this


assdaasdsd

If you’re new to this, it’s recommended to start by watching a tutorial on data types in Lua.

  • pedType should be an integer (e.g., 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and so on).
  • modelHash should be a string (e.g., “g_m_y_famdnf_01”).

Also, you can simply use local ped = CreatePed() instead of local retval Entity = CreatePed().

image
still erroring

thats because you have an end for no reason you have to remove it maybe you try something like this :

Citizen.CreateThread(function()
    local isPedLoaded = false
    local npc = nil
    
    while true do 
        local sleep = 1000
        
        local pedCoords = GetEntityCoords(PlayerPedId())
        local npcPosition = vector4(0.0, 0.0, 0.0, 0.0) -- Put your ped's coordinates here
        local distance = #(pedCoords - vector3(npcPosition.x, npcPosition.y, npcPosition.z))

        if distance <= 8.0 then 
            sleep = 0

            local pedModel = GetHashKey("your_model") -- Change "your_model" to the model you want
            
            if not isPedLoaded then 
                RequestModel(pedModel)
                
                while not HasModelLoaded(pedModel) do 
                    Wait(100)
                end

                local _, zCoord = GetGroundZFor_3dCoord(npcPosition.x, npcPosition.y, npcPosition.z) -- Ground check for accurate Z-coordinate
                
                npc = CreatePed(4, pedModel, npcPosition.x, npcPosition.y, zCoord, npcPosition.w, false, false)
                
                FreezeEntityPosition(npc, true)
                SetEntityInvincible(npc, true)
                SetBlockingOfNonTemporaryEvents(npc, true)
                isPedLoaded = true
            end

            if distance <= 1.0 then 
                ESX.ShowNotification("Press E to interact") -- Example notification

                if IsControlJustPressed(0, 38) then 
                    -- Add interaction logic here
                end
            end
        else 
            if npc ~= nil then 
                DeleteEntity(npc)
                SetModelAsNoLongerNeeded(pedModel)
                isPedLoaded = false
                npc = nil 
            end
        end

        Citizen.Wait(sleep)
    end
end)

ok so how do i make it to where i pay them a certain amount of money and they give me my items


this is what i see rn no errors n all

did you change the coords and the model name?

yes i did

it might be the model some models dont work i tried it like that and it workt just fine :


Citizen.CreateThread(function()
    local isPedLoaded = false
    local npc = nil
    
    while true do 
        local sleep = 1000
        
        local pedCoords = GetEntityCoords(PlayerPedId())
        local npcPosition = vector4(223.8314, -807.4439, 30.6175, 163.3645) -- Put your ped's coordinates here
        local distance = #(pedCoords - vector3(npcPosition.x, npcPosition.y, npcPosition.z))

        if distance <= 8.0 then 
            sleep = 0

            local pedModel = GetHashKey("a_f_y_beach_01") -- Change "your_model" to the model you want
            
            if not isPedLoaded then 
                RequestModel(pedModel)
                
                while not HasModelLoaded(pedModel) do 
                    Wait(100)
                end

                local _, zCoord = GetGroundZFor_3dCoord(npcPosition.x, npcPosition.y, npcPosition.z) -- Ground check for accurate Z-coordinate
                
                npc = CreatePed(4, pedModel, npcPosition.x, npcPosition.y, zCoord, npcPosition.w, false, false)
                
                FreezeEntityPosition(npc, true)
                SetEntityInvincible(npc, true)
                SetBlockingOfNonTemporaryEvents(npc, true)
                isPedLoaded = true
            end

            if distance <= 1.0 then 
                ESX.ShowHelpNotification("Press E to interact") -- Example notification

                if IsControlJustPressed(0, 38) then 
                    -- Add interaction logic here
                end
            end
        else 
            if npc ~= nil then 
                DeleteEntity(npc)
                SetModelAsNoLongerNeeded(pedModel)
                isPedLoaded = false
                npc = nil 
            end
        end

        Citizen.Wait(sleep)
    end
end)

Ok ima try this when I get home, can we talk on d cord to speed up the responses? Add kjay3800_

added you

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