Jump to content

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 ;)

Link to comment
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)

 

Link to comment
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
Link to comment
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) :| 

Link to comment

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)

 

Link to comment
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

Link to comment

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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
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...