Jump to content

HELP! cant get all account data


synskidz

Recommended Posts

Posted (edited)
function onregenstart()
setTimer( healthup, 7000, 0, getElementsByType( "player" ))
end
addEventHandler( "onResourceStart", getResourceRootElement(getThisResource()), onregenstart )
 
function healthup(thePlayer)
for k, players in ipairs(getElementsByType( "player" )) do
if(getAccountData(players, "regen") == true) then
local phealth = getElementHealth( players )
setElementHealth( players, phealth + 5 )
end
end
end

it says "WARNING: remek\remek.lua:8: Bad argument @ 'getAccountData' [Expected account at argument 1, got player]"

i want to all player with the account data named "regen" that have "true" value can regen health. and player data with false value on the account cant regen the health

 














Edited by synskidz
Posted (edited)
function onRegenStart( )
	setTimer( healthUp, 7000, 0 )
end
addEventHandler( "onResourceStart", getRootElement( ), onRegenStart )
 
function healthUp( )
	for index, value in pairs ( getElementsByType( "player" ) ) do 
		if ( getAccountData( getPlayerAccount( value ), "regen" ) == true ) then
			local playerHealth = getElementHealth( value )
			setElementHealth( value, playerHealth + 5 )
		end
	end 
end

This should work( not tested )

Please,when naming a function, use capital letters when another word starts( Ex: yourFunctionName, NOT yourfunctionname ) because it looks better and helps you understand better what does that function do.

Edited by *BeaT*
Add some information
  • Like 1
Posted

Using a timer is a very bad idea and specially if it was in the sevrer side also looping all the players it's really not recommended so you can try the next.
I will recommend using 'onPlayerDamage' and check if the health was < than it should be add more health for the damaged player.

Posted
15 minutes ago, *BeaT* said:

function onRegenStart( )
	setTimer( healthUp, 7000, 0 )
end
addEventHandler( "onResourceStart", getRootElement( ), onRegenStart )
 
function healthUp( )
	for index, value in pairs ( getElementsByType( "player" ) ) do 
		if ( getAccountData( getPlayerAccount( value ), "regen" ) == true ) then
			local playerHealth = getElementHealth( value )
			setElementHealth( value, playerHealth + 5 )
		end
	end 
end

This should work( not tested )

Please,when naming a function, use capital letters when another word starts( Ex: yourFunctionName, NOT yourfunctionname ) because it looks better and helps you understand better what does that function do.

Big Thanks Bro, u r genius. it work thanks

Posted

I don't think a single timer is gonna blow up the server, it was made to handle such things, unless it's running on a potato. In some cases you just don't have a work around, but for this specific problem, you really should attach your function to an event, like onPlayerDamage as iPrestege said, only use timers when there is no other way.

Posted (edited)
11 minutes ago, pa3ck said:

I don't think a single timer is gonna blow up the server, it was made to handle such things, unless it's running on a potato. In some cases you just don't have a work around, but for this specific problem, you really should attach your function to an event, like onPlayerDamage as iPrestege said, only use timers when there is no other way.

thanks for ur advice dude, i'll find another way

Edited by synskidz
Posted (edited)

NOT TESTED! (Not sure if is gonna work )

function startRegenOnDamage( attacker, attackerWeapon, bodyPart, loss )
	if ( getAccountData( getPlayerAccount( source ), "regen" ) == true ) then 
		startRegen( source )
	end 
end 
addEventHandler( "onPlayerDamage", getRootElement( ), startRegenOnDamage )

function startRegen( thePlayer )
	local playerHealth = getElementHealth( thePlayer )
	local timesToExecute = math.ceil( ( 100 - playerHealth ) / 5 )
	setTimer( healUpThePlayer, 7000, timesToExecute, thePlayer )
end

function healUpThePlayer( playerToHeal )
	if ( playerToHeal and getElementType( playerToHeal ) == "player" ) then 
		local regenAmount = 100 - getElementHealth( playerToHeal )
		if ( regenAmount < 5 ) then 
			setElementHealth( playerToHeal, 100 )
			return
		else 
			setElementHealth( playerToHeal, getElementHealth( playerToHeal ) + 5 )
		end 
	end 
end 

When a player gets damaged, the script will test if the player has the regen data.If yes, the script will calculate the needed times to execute healUpThePlayer function( Ex: if the player has now 75 health, the function will be executed 5 times )

Edited by *BeaT*
What the script should do
Posted

Version 2.0:

--CLIENT
local res = getResourceRootElement( getThisResource() )
local tick

function startStuff()
  tick = getTickCount()
end
addEventHandler( "onClientResourceStart", res, startStuff )

function renderStuff()
   local now = getTickCount()
   if ( now - tick >= 7000 ) then
    triggerServerEvent( "onTime", localPlayer, localPlayer )
    tick = getTickCount()
   end
end
addEventHandler( "onClientRender", root, renderStuff )
--SERVER

addEvent( "onTime", true )

function handleTime( thePlayer )
   if ( thePlayer ) then
     local a = getPlayerAccount( thePlayer )
     if ( a ) then
       local b = getAccountData( a, "regen" )
       if ( b == true ) then
        local hp = getElementHealth( thePlayer )
        if ( hp < 100 ) then
          local n_hp = ( hp + 5 > 100 ) and 100 or hp + 5
          setElementHealth( thePlayer, n_hp )
        end
       end
     end
   end
end
addEventHandler( "onTime", root, handleTime )

 

Posted

None of these solutions make sense... What you should do:

Server: onPlayerLogin -> setElementData(source, "regen", true) | onPlayerLogout -> setElementData(source, "regen", false)

Client: timer(function if getElementData(localPlayer, "regen") setElementHealth(..), 7000, 0 )

Or, if you don't want an endless timer, onClientElementDataChange -> is it "regen" that changed? Yes... is it turned on? Yes... start time timer | Is it turned off? Yes... check if there's a timer, if there's, kill it.

 

Having the timer server side, not efficient... Having triggerServerEvent for every player every 7 secs? Highly inefficient...

Posted
49 minutes ago, pa3ck said:

None of these solutions make sense... What you should do:

Server: onPlayerLogin -> setElementData(source, "regen", true) | onPlayerLogout -> setElementData(source, "regen", false)

Client: timer(function if getElementData(localPlayer, "regen") setElementHealth(..), 7000, 0 )

Or, if you don't want an endless timer, onClientElementDataChange -> is it "regen" that changed? Yes... is it turned on? Yes... start time timer | Is it turned off? Yes... check if there's a timer, if there's, kill it.

 

Having the timer server side, not efficient... Having triggerServerEvent for every player every 7 secs? Highly inefficient...

Let me tell you sir intelligent that elementData is the worst case you can use. Why do you use onPlayerLogin for making some elementData assingments? Please check yourself before checking others one.

Posted (edited)
1 hour ago, pa3ck said:

Having triggerServerEvent for every player every 7 secs?

@Simple01 Just my opinion, but i think that a server side timer is still better than a trigger every 7 seconds. For the rest, i agree.

PS: still i hate timers

Edited by LoPollo
  • Like 2
Posted (edited)

I'm really sorry that that's what you understood from my post. I didn't mean to mock you, or @LoPollo (I know he is in fact a very good scripter himself), I just wanted to make my point. Why I used onPlayerLogin? Because in his code he is checking getAccountData so probably he is saving something there, don't you think? Probably only specific players could regenerate their hp... don't know, it's not my code. As I said, I didn't mean to mock you, but can you please elaborate on elementData being the worst? Over triggerServerEvent every 7 secs? You are stating that changing elementData when user logs in and logs out, meaning 2 "server operations" in probably at least 30 mins is worse than your code, which makes "server operations" in every 7 second, that is 1800 / 7 = 257 in 30 mins. My main programming language is C# and JavaScript, I would be glad if you could explain it to me, I'm probably just like you, trying to learn every day, whenever I can. Thanks @Simple01.

Edited by pa3ck
  • Like 2

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