Jump to content

Bank credit system problems


Recommended Posts

Hello everyone! Here is my code:

addEventHandler("onResourceStart", resourceRoot,
    function()
        db = dbConnect("sqlite", "credits.db")
		db2 = dbConnect("sqlite", ":salo/database.db")
        --dbExec(db, "TRUNCATE TABLE CreditList") -- очистка
        dbExec(db, "CREATE TABLE IF NOT EXISTS CreditList (Player, Type, Amount, TimeLeft)")
    end
)
--[[
addEventHandler("onResourceStop",resourceRoot,
	function()
		
	end
)]]

addEventHandler("onPlayerQuit",getRootElement(),function()
	if getElementData(source,"isCredit") and getElementData(source,"CreditAmount") then
		dbExec(db,"UPDATE CreditList SET Amount = ?, TimeLeft = ? WHERE Player = ?",getElementData(source,"CreditAmount"),getTimerDetails(timers[source]),getAccountName(getPlayerAccount(source)))
		killTimer(timers[source])
	end
end)
--[[
addCommandHandler("hel",function(localplayer)
	setElementData(localplayer,"isCredit",false)
	setElementData(localplayer,"CreditAmount",0)
end)]]

addEventHandler("onPlayerLogin",getRootElement(),function()
	--setTimer(
	--function(source)
		local acc = getPlayerAccount(source)
		if getAccountData(acc,"isCredit") then
			dat = dbPoll(dbQuery(db, "SELECT * FROM CreditList WHERE Player = ?", getAccountName(getPlayerAccount(source))), -1)
			givecr(source,dat["Type"],dat["Amount"],dat["TimeLeft"])
			dat = nil
		end
		acc = nil
		setElementData(source,"creditMultiplier",credit_)
		setElementData(source,"depositMultiplier",deposit_)
	--end,50,1)
end)

function givecr(player_,type_,amount,timeleft_timer)
	--triggerClientEvent("givePlayerCreditTimer",player_) -- даёт таймер игроку, который будет сохраняться при выходе и выдаваться при входе игрока
	setElementData(player_,"isCredit",true)
	setElementData(player_,"CreditAmount",amount) -- не забыть проверку на макс. количество денег, которые можно взять в долг.
	if not timeleft_timer then
		--tim = creditDays*86400000
		tim = creditDays*1000
	else
		tim = timeleft_timer
	end
	outputChatBox(11)
	timers[player_] = setTimer(
	function()
		outputChatBox(22)
		if type_ == "house" then
			--exports.res-houses -- забрать дом
		elseif type_ == "vehicle" then
			outputChatBox(33)
			--exports.salo -- забрать авто
			dbExec(db2, "DELETE FROM VehicleList WHERE Account = ? AND isCreditCar = ?", getAccountName(getPlayerAccount(player_)), true)
			for i, v in ipairs(getElementsByType("vehicle")) do 
				if getElementData(v,"ownercar") == getAccountName(getPlayerAccount(player_)) then
					destroyElement(v)
					outputChatBox(44)
				end
			end
			exports["res-houses"]:calculateCarLimit(player_)
			setElementData(player_,"carCount",getElementData(player_,"carCount")-1)
			outputChatBox("Вы не успели вовремя оплатить кредит, поэтому государство отняло у вас авто.",player_)
		elseif type_ == "money" then
			-- coming soon
		end
	end,tim,1)
	player_ = nil
	type_ = nil
	amount = nil
	timeleft_timer = nil
	tim = nil
end

Each time i login(even from new account) i see in chat 11 and then 22 after 3-5 seconds. I want to sell a vehicle temporary. So I create a timer ( 7 seconds) and when it's over, script removes database entry with this car and with timer info in another database. I feel discouraged right now and i don't know what to do\

Link to comment
18 hours ago, myonlake said:

So what exactly is the problem?

variable player_ lose it's value in timer or i'm doing something wrong

On 10.05.2018 at 10:02, Juuve said:

player_ = source
timers[player_] = setTimer( -- player_ is not nil
	function()
		type_ = getElementData(player_,"CreditType") -- player_ becomes nil here
		if type_ == "house" then

I think that's better explanation. p.s. everything is server-side

 

Link to comment
timers[player_] = setTimer(
  function(player) -- collect the player_ sent over through the callback
    local type_ = getElementData(player, "CreditType") -- player_ is now referred as player in this scope
    if type_ == "house" then
      -- ...
    end
  end, tim, 1, player_
)

Send player_ to the callback function through a callback argument and collect it in the parameters list of the callback function.

Edited by MrTasty
  • Thanks 1
Link to comment
On 12.05.2018 at 00:57, MrTasty said:

timers[player_] = setTimer(
  function(player) -- collect the player_ sent over through the callback
    local type_ = getElementData(player, "CreditType") -- player_ is now referred as player in this scope
    if type_ == "house" then
      -- ...
    end
  end, tim, 1, player_
)

Send player_ to the callback function through a callback argument and collect it in the parameters list of the callback function.

yeah, i missed arguments at the end of function. I have already fixed it but thanks anyway xD

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...