Turbe$Z

How to save player coins to account?

Recommended Posts

function setAccountCoinValue(player,amount)
  if not player or not (isElement(player)) then return false end --Prevents errors from occuring when the player element isn't defined.
  local account = getPlayerAccount(player)
  
  --Make sure the player is logged in
  if isGuestAccount(account) then return false end
  
  --Set the coins value in the player's account
  setAccountData(account,"coins",amount)
  return true
end

function getAccountCoinValue(player)
  if not player or not (isElement(player)) then return false end
  
  local account = getPlayerAccount(player)
  
  if isGuestAccount(account) then return false end
  
  return getAccountData(account,"coins") or 0
end

Create a function to call these functions when the player joins, quits, and when the player's coin value is changed to save it to their account ;)

Share this post


Link to post
5 hours ago, 1B0Y said:

function setAccountCoinValue(player,amount)
  if not player or not (isElement(player)) then return false end --Prevents errors from occuring when the player element isn't defined.
  local account = getPlayerAccount(player)
  
  --Make sure the player is logged in
  if isGuestAccount(account) then return false end
  
  --Set the coins value in the player's account
  setAccountData(account,"coins",amount)
  return true
end

function getAccountCoinValue(player)
  if not player or not (isElement(player)) then return false end
  
  local account = getPlayerAccount(player)
  
  if isGuestAccount(account) then return false end
  
  return getAccountData(account,"coins") or 0
end

Create a function to call these functions when the player joins, quits, and when the player's coin value is changed to save it to their account ;)

i added events, but doesn't working, why? :S 

function setAccountCoinValue(player,amount)
	if not player or not (isElement(player)) then return false end
	local account = getPlayerAccount(player)

	if isGuestAccount(account) then return false end

	setAccountData(account,"coins",amount)
	return true
end

function getAccountCoinValue(player)
	if not player or not (isElement(player)) then return false end
	
	local account = getPlayerAccount(player)
	
	if isGuestAccount(account) then return false end
	
	return getAccountData(account,"coins") or 0
end

addEventHandler("onPlayerJoin",root,setAccountCoinValue)
addEventHandler("onPlayerQuit",root,getAccountCoinValue)
addEventHandler("onResourceStart",resourceRoot,setAccountCoinValue)

 

Share this post


Link to post
addEventHandler("onPlayerLogin", root, function()
    local coin = getAccountCoinValue(source) or 0
    setElementData(source, "moneycoins", coin)
end)

addEventHandler("onPlayerQuit", root, function()
    local coin = getElementData(source, "moneycoins") or 0
    setAccountCoinValue(source, coin)
end)

addEventHandler("onResourceStart", resourceRoot, function()
    for _, player in pairs(getElementsByType("player")) do
        local coin = getAccountCoinValue(player)
        if coin then
            setElementData(player, "moneycoins", coin)
        end
    end
end)

 

Edited by ZoRRoM
  • Like 1

Share this post


Link to post
11 minutes ago, ZoRRoM said:

addEventHandler("onPlayerLogin", root, function()
    local coin = getAccountCoinValue(source) or 0
    setElementData(source, "moneycoins", coin)
end)

addEventHandler("onPlayerQuit", root, function()
    local coin = getElementData(source, "moneycoins") or 0
    setAccountCoinValue(source, coin)
end)

addEventHandler("onResourceStart", resourceRoot, function()
    for _, player in pairs(getElementsByType("player")) do
        local coin = getAccountCoinValue(player)
        if coin then
            setElementData(player, "moneycoins", coin)
        end
    end
end)

 

attempt to call global 'getAccountCoinValue' (a nil value) :| 

Share this post


Link to post

because this function does not exist. Try this: 

addEventHandler("onPlayerLogin", root, function()
    local coin = getAccountData(source, "coins") or 0
    setElementData(source, "moneycoins", tonumber(coin))
end)

addEventHandler("onPlayerQuit", root, function()
    local coin = getElementData(source, "moneycoins") or 0
    setAccountData(source, "coins", tonumber(coin))
end)

addEventHandler("onResourceStart", resourceRoot, function()
    for _, player in pairs(getElementsByType("player")) do
        local coin = getAccountData(player, "coins")
        if coin then
            setElementData(player, "moneycoins", tonumber(coin))
        end
    end
end)

 

Share this post


Link to post
8 minutes ago, Simi23 said:

because this function does not exist. Try this: 


addEventHandler("onPlayerLogin", root, function()
    local coin = getAccountData(source, "coins") or 0
    setElementData(source, "moneycoins", tonumber(coin))
end)

addEventHandler("onPlayerQuit", root, function()
    local coin = getElementData(source, "moneycoins") or 0
    setAccountData(source, "coins", tonumber(coin))
end)

addEventHandler("onResourceStart", resourceRoot, function()
    for _, player in pairs(getElementsByType("player")) do
        local coin = getAccountData(player, "coins")
        if coin then
            setElementData(player, "moneycoins", tonumber(coin))
        end
    end
end)

 

now when i have xy coin, and i reconnect, and login, the coins reduce to 0 :S

Share this post


Link to post

And there is no errors or warns in the debugscript?

 

 

Share this post


Link to post
1 minute ago, Simi23 said:

And there is no errors or warns in the debugscript?

 

 

c_server.lua:13: Bad argument @ 'getAccountData' [expected account at argument 1, got player]

Share this post


Link to post

Try this:

addEventHandler("onPlayerLogin", root, function()
    local acc = getPlayerAccount(source)
    local coin = getAccountData(acc, "coins") or 0
    setElementData(source, "moneycoins", tonumber(coin))
end)

addEventHandler("onPlayerQuit", root, function()
    local acc = getPlayerAccount(source)
    local coin = getElementData(source, "moneycoins") or 0
    setAccountData(acc, "coins", tonumber(coin))
end)

addEventHandler("onResourceStart", resourceRoot, function()
    for _, player in pairs(getElementsByType("player")) do
        local acc = getPlayerAccount(source)
      	local coin = getAccountData(acc, "coins")
        if coin then
            setElementData(player, "moneycoins", tonumber(coin))
        end
    end
end)
  • Like 1
  • Thanks 1

Share this post


Link to post
3 minutes ago, Simi23 said:

Try this:


addEventHandler("onPlayerLogin", root, function()
    local acc = getPlayerAccount(source)
    local coin = getAccountData(acc, "coins") or 0
    setElementData(source, "moneycoins", tonumber(coin))
end)

addEventHandler("onPlayerQuit", root, function()
    local acc = getPlayerAccount(source)
    local coin = getElementData(source, "moneycoins") or 0
    setAccountData(acc, "coins", tonumber(coin))
end)

addEventHandler("onResourceStart", resourceRoot, function()
    for _, player in pairs(getElementsByType("player")) do
        local acc = getPlayerAccount(source)
      	local coin = getAccountData(acc, "coins")
        if coin then
            setElementData(player, "moneycoins", tonumber(coin))
        end
    end
end)

now working the script, save my coins, thanks :D but i got a warning: "Bad argument @ 'getAccountData' [Expected account at argument 1, got boolean]" Why got this warning?

  • Thanks 1

Share this post


Link to post

Your welcome, I dont know why you get that error, but i think it's fine cuz its working. #wearethevr

  • Like 1

Share this post


Link to post
2 minutes ago, Simi23 said:

Your welcome, I dont know why you get that error, but i think it's fine cuz its working. #wearethevr

Lol, and when i join the game, the counter does not appear jut when i logged in, and got this warning @ Bad argument @ 'dxDrawText' [Expected string at argument 1, got nil] How to fix this? ..:s

This is the client code:

local screenW, screenH = guiGetScreenSize()
local x, y = (screenW/1024), (screenH/768)

local size = y*1.50 

function drawText()
	local playerCoins = getPlayerCoin(getLocalPlayer());
	-- local gPlayerCoins = string.format("%08d", playerCoins);
		
        dxDrawRectangle(screenW * 0.6972, screenH * 0.0711, screenW * 0.1500, screenH * 0.0256, tocolor(0, 0, 0, 170), false)
        dxDrawText("Coin:", screenW * 0.7000, screenH * 0.0711, screenW * 0.7431, screenH * 0.0967, tocolor(255, 255, 255, 255), 1.00, "default-bold", "left", "center", false, false, false, false, false)
        dxDrawText(playerCoins, screenW * 0.7431, screenH * 0.0711, screenW * 0.8472, screenH * 0.0967, tocolor(255, 255, 255, 255), 1.00, "default-bold", "center", "center", false, false, false, false, false)	
end
addEventHandler("onClientRender", root, drawText)


function getPlayerCoin()
    local data = getElementData(getLocalPlayer(), "moneycoins");
    thePoints = tonumber(data);
	return thePoints
end

fileDelete("c_client.lua")

And yeah, #WeAreTheVR :D 

  • Thanks 1

Share this post


Link to post

Instead of row 7, use

local playerCoins = tostring(getElementData(getLocalPlayer(), "moneycoins"))

 

  • Like 1
  • Thanks 1

Share this post


Link to post
2 minutes ago, Simi23 said:

Instead of row 7, use


local playerCoins = tostring(getElementData(getLocalPlayer(), "moneycoins"))

 

Now I do not get warning(s), but why show false instead of 0? :o 

  • Thanks 1

Share this post


Link to post
local playerCoins = tostring(getElementData(getLocalPlayer(), "moneycoins")) or tostring(0)

Maybe this will do it

  • Like 1
  • Thanks 1

Share this post


Link to post
1 minute ago, Simi23 said:

local playerCoins = tostring(getElementData(getLocalPlayer(), "moneycoins")) or tostring(0)

Maybe this will do it

No, same kind O.o 

  • Thanks 1

Share this post


Link to post
local screenW, screenH = guiGetScreenSize()
local x, y = (screenW/1024), (screenH/768)

local size = y*1.50 

function drawText()
		local playerCoins = getElementData(getLocalPlayer(), "moneycoins")
	    if not playerCoins then
    		playerCoins = "0"
    	end
		
        dxDrawRectangle(screenW * 0.6972, screenH * 0.0711, screenW * 0.1500, screenH * 0.0256, tocolor(0, 0, 0, 170), false)
        dxDrawText("Coin:", screenW * 0.7000, screenH * 0.0711, screenW * 0.7431, screenH * 0.0967, tocolor(255, 255, 255, 255), 1.00, "default-bold", "left", "center", false, false, false, false, false)
        dxDrawText(playerCoins, screenW * 0.7431, screenH * 0.0711, screenW * 0.8472, screenH * 0.0967, tocolor(255, 255, 255, 255), 1.00, "default-bold", "center", "center", false, false, false, false, false)	
end
addEventHandler("onClientRender", root, drawText)


function getPlayerCoin()
    local data = getElementData(getLocalPlayer(), "moneycoins");
    thePoints = tonumber(data);
	return thePoints
end

fileDelete("c_client.lua")

 

  • Like 1
  • Thanks 1

Share this post


Link to post
2 minutes ago, Simi23 said:

local screenW, screenH = guiGetScreenSize()local x, y = (screenW/1024), (screenH/768)local size = y*1.50 function drawText()		local playerCoins = getElementData(getLocalPlayer(), "moneycoins")	    if not playerCoins then    		playerCoins = "0"    	end		        dxDrawRectangle(screenW * 0.6972, screenH * 0.0711, screenW * 0.1500, screenH * 0.0256, tocolor(0, 0, 0, 170), false)        dxDrawText("Coin:", screenW * 0.7000, screenH * 0.0711, screenW * 0.7431, screenH * 0.0967, tocolor(255, 255, 255, 255), 1.00, "default-bold", "left", "center", false, false, false, false, false)        dxDrawText(playerCoins, screenW * 0.7431, screenH * 0.0711, screenW * 0.8472, screenH * 0.0967, tocolor(255, 255, 255, 255), 1.00, "default-bold", "center", "center", false, false, false, false, false)	endaddEventHandler("onClientRender", root, drawText)function getPlayerCoin()    local data = getElementData(getLocalPlayer(), "moneycoins");    thePoints = tonumber(data);	return thePointsendfileDelete("c_client.lua")

 

thank you :D

  • Like 1
  • Thanks 1

Share this post


Link to post
2 hours ago, Turbo777 said:

attempt to call global 'getAccountCoinValue' (a nil value) :| 

Because i wrote it for 1B0Y's code. You should use both.

  • Like 1

Share this post


Link to post

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.