Jump to content

attempt to get length of global ' ' (a nil value)


_RuiGy

Recommended Posts

Hi,

I am trying to make a script to save how much pizzas the player found.

But i am getting some errors.

This is my code:

--server-side 
function onPlayerQuit() 
    local playerAccount = getPlayerAccount(source) 
    if (playerAccount) then 
        for iii = 1, #nPizzaFound do 
            setAccountData(playerAccount, "xtreme.pizza" .. iii, nPizzaFound[iii]) 
        end 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuit) 
  
function onPlayerLogin() 
    local playerAccount = getPlayerAccount(source) 
    if (playerAccount) then 
        for iii = 1, #nPizzaFound do 
            local playerPizzas = getAccountData(playerAccount, "xtreme.pizza" .. iii) 
            if (playerPizzas) then 
                nPizzaFound[iii] = playerPizzas 
            end 
        end 
    end 
end 
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin) 

nPizzaFound is definied in cliente-side

--cliente-side 
local nPizzaPickup = { 
    createPickup ( -1088.5999755859, 428.20001220703, 13.60000038147, 3, 1582 ), 
    createPickup ( -1067.5, 407.20001220703, 13.60000038147, 3, 1582 ) 
} 
-------------------------------------------------------------------------------------- 
nPizzaFound = {} 
local nTotalFound = 0 
--------------------- 
function createPizzaPickup ( ) 
    outputChatBox ( "Find the pizzas!" ) 
end 
addEventHandler ( "onClientResourceStart", getResourceRootElement ( getThisResource ( ) ), createPizzaPickup ) 
-------------------------------------------------------------------------------------------------------------- 
function onPlayerPickupPizza ( thePickup ) 
    for iii = 1, #nPizzaPickup do 
        if thePickup == nPizzaPickup[iii] then 
            if nPizzaFound[iii] == true then 
                outputChatBox ( "You already found this pizza." ) 
                return true 
            end 
            outputChatBox ( "You found the pizza " .. iii .. ". Here, have some money." ) 
            givePlayerMoney ( 500 ) 
            nPizzaFound[iii] = true 
            nTotalFound = nTotalFound + 1 
            if nTotalFound == #nPizzaFound then 
                outputChatBox ( "Congratulations! You found all pizzas. Here, have more money." ) 
                givePlayerMoney ( 1500 ) 
            end 
        end 
    end 
end 
addEventHandler ( "onClientPlayerPickupHit", localPlayer, onPlayerPickupPizza ) 

and this is the error i am facing:

findPizzaMG_S.lua:5: attempt to get length of global 'nPizzaFound' (a nil value) 

Thanks for any help.

Link to comment

To transfer the data between client and server, you need to use:

setElementData 

or

triggerClientEvent 
triggerServerEvent 

But I don't see any reason why you would need to use client-side script at all in this case. Setting money client-side does not change it server-side.

Link to comment
To transfer the data between client and server, you need to use:
setElementData 

or

triggerClientEvent 
triggerServerEvent 

But I don't see any reason why you would need to use client-side script at all in this case. Setting money client-side does not change it server-side.

Thank you, i changed my script to server-side. It works now.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...