Jump to content

Scoreboard


xbenny2506x

Recommended Posts

  • 2 weeks later...

I use now the script from mta 1.0 "scores.zip" and i have add some new things but i have problems with add the money.

I have test many things but nothing work :(

Can someone say me how i can add the player money?

Here is the original script not that what i have use atm, but i must only know how i can add the money...

local root = getRootElement()
local scoresRoot = getResourceRootElement(getThisResource())
local scoreboard = getResourceFromName("scoreboard")
 
local scoreColumns = {"Kills", "Deaths", "Self", "Ratio", "Status", "Money"}
local isColumnActive = {}
 
local KDR_DECIMAL_PLACES = 2
 
--http://lua-users.org/wiki/SimpleRound
local function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
 
local function setScoreData (element, column, data)
if isColumnActive[column] then
setElementData(element, column, data)
end
end
 
local function resetScores (element)
setScoreData(element, "Kills", 0)
setScoreData(element, "Deaths", 0)
setScoreData(element, "Self", 0)
setScoreData(element, "Ratio", "-")
setScoreData(element, "Money", "0")
local status = ""
if isPedDead(element) then
	status = "Dead"
end
setScoreData(element, "status", status)
end
 
 
local function updateRatio (element)
local Deaths = getElementData(element, "Deaths")
if Deaths == 0 then
	setScoreData(element, "Ratio", "-")
else
local kdr = round(getElementData(element, "Kills") / Deaths, KDR_DECIMAL_PLACES)
	setScoreData(element, "ratio", tostring(kdr))
end
end
 
function updateActiveColumns ()
for i, column in ipairs(scoreColumns) do
if get(column) then
		isColumnActive[column] = true
call(scoreboard, "addScoreboardColumn", column)
elseif isColumnActive[column] then
		isColumnActive[column] = false
call(scoreboard, "removeScoreboardColumn", column)
end
end
end
 
addEventHandler("onResourceStart", scoresRoot,
function ()
	updateActiveColumns()
for i, player in ipairs(getElementsByType("player")) do
		resetScores(player)
end
end
)
 
addEventHandler("onResourceStop", scoresRoot,
function ()
for i, column in ipairs(scoreColumns) do
if isColumnActive[column] then
call(scoreboard, "removeScoreboardColumn", column)
end
end
end
)
 
addEventHandler("onPlayerJoin", root,
function ()
	resetScores(source)
end
)
 
addEventHandler("onPlayerWasted", root,
function (ammo, killer, weapon)
if killer then
if killer ~= source then
-- killer killed victim
			setScoreData(killer, "Kills", getElementData(killer, "Kills") + 1)
			setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1)
if isColumnActive["ratio"] then
				updateRatio(killer)
				updateRatio(source)
end
else
-- victim killed himself
			setScoreData(source, "self", getElementData(source, "self") + 1)
end
else
-- victim died
		setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1)
if isColumnActive["ratio"] then
			updateRatio(source)
end
end
 
	setScoreData(source, "status", "Dead")
end
)
 
addEventHandler("onPlayerSpawn", root,
function ()
	setScoreData(source, "status", "")
end
)
 
addCommandHandler("score",
function (player)
if player then
for i, column in ipairs(scoreColumns) do
if column == "status" then
break
end
if isColumnActive[column] then
call(scoreboard, "addScoreboardColumn", column)
outputConsole(column .. ": " .. getElementData(player, column), player)
end
end
end
end
)

Link to comment

After using setPlayerMoney or givePlayerMoney, try this:

setElementData(***,"Money",tostring(getPlayerMoney(***)))

NOTE: Replace *** with the player element.

So for instance:

function giveMeMoney(player)
givePlayerMoney(player,500)  -- Give money to the player, $500 for instance
setElementData(player,"Money",getPlayerMoney(player))  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
outputChatBox("You have been given $500",player,0,255,0) -- Output a message, for pointlessness' sake
end
addCommandHandler("gimmemoney",giveMeMoney)

Edited by Guest
Link to comment
After using setPlayerMoney or givePlayerMoney, try this:
setElementData(***,"Money",tostring(getPlayerMoney(***)))

NOTE: Replace *** with the player element.

So for instance:

function giveMeMoney(player)
givePlayerMoney(player,500)  -- Give money to the player, $500 for instance
setElementData(player,"Money",getPlayerMoney(player),false)  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
outputChatBox("You have been given $500",player,0,255,0) -- Output a message, for pointlessness' sake
end
addCommandHandler("gimmemoney",giveMeMoney)

As far as I know, scoreboard updates board with onClientElementDataChange event, that is whenever element data is changed (client must know about it)... You're not syncing player's "Money" data with client so scoreboard will not be updated at all.

Link to comment
As far as I know, scoreboard updates board with onClientElementDataChange event, that is whenever element data is changed (client must know about it)... You're not syncing player's "Money" data with client so scoreboard will not be updated at all.

Oops... hehe... D:

I fear I'm kinda used to almost always setting that to false by now.

Edited my example.

Link to comment

ok i see it is not so eays :)

But use now:

function trans (player)
 
setElementData(player,"Money",tostring(getPlayerMoney(player)))
 
end
 
addEventHandler("onPlayerWasted", getRootElement(), 
function (player)
if player then
setElementData(source,"Money",getPlayerMoney(source))  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
outputChatBox("Datasend work",source,0,255,0) -- Output a message, for pointlessness' sake
 
end
end
)

The Player money Update when the player is death, its not all the time but for the first time i think its ok, i work on that what 50p have say. But for that i must read a lot in wiki, and test this idea later :)

When somone found a bug, error or something else or i have make something wrong plz say me this :) THX

Link to comment

I got something better.

Put this on top of your script:

-- Rewrite the function givePlayerMoney;
_givePlayerMoney = givePlayerMoney;
 
function givePlayerMoney(player, ammount)
   _givePlayerMoney(player, ammount);
setElementData(player, "Money", getPlayerMoney(player));
end
 
-- Rewrite the function takePlayerMoney;
_takePlayerMoney = takePlayerMoney;
 
function takePlayerMoney(player, ammount)
   _takePlayerMoney(player, ammount);
setElementData(player, "Money", getPlayerMoney(player));
end
 
-- Rewrite the function setPlayerMoney;
_setPlayerMoney = setPlayerMoney;
 
function setPlayerMoney(player, ammount)
   _setPlayerMoney(player, ammount);
setElementData(player, "Money", getPlayerMoney(player));
end
 
-- Usage
function playerIsDead()
takePlayerMoney(source, 500);
outputChatBox("Doctor: You ain't got insurance, so i took the hospital bill from you!"); -- Ooopsie made a mistake, i needed to close with " and not with '.
end
 
addEventHandler("onPlayerWasted", getRootElement(), playerIsDead);

Now it ain't that difficult anymore!!!!

Good luck with it.

Link to comment

I have now a other problem, i want save the kills and death.

My money save work very good, but the kill and death make me many problems.

I have use the script https://forum.multitheftauto.com/viewtop ... 32#p293132 and have change it so that i can save the kills and deaths.

But i get a error:

ERROR: ...rver/mods/deathmatch/resourcecache/scores/scores.lua:14: attempt to get length of local 'player' (a nil value)

ERROR: ...rver/mods/deathmatch/resourcecache/scores/scores.lua:169: attempt to perform arithmetic on a nil value

this is my script:

New add new db:

function dbaddkilldeath ()
executeSQLCreateTable ( "killdeath", "kill INTEGER, death INTEGER, player TEXT" )
end
addCommandHandler ("dbaddkill", dbaddkilldeath)

Read db:

function onjoindbkill (player) 
local sourcename = getPlayerName (source) 
       killdatalesen = executeSQLQuery("SELECT kill FROM killdeath WHERE player=?", sourcename)
if(#player == 0) then  
executeSQLInsert ( "killdeath", "'1', '1', '" .. sourcename .. "'" )
else
               killsplayer = killdatalesen[1].kill
 
               deathdatalesen = executeSQLQuery("SELECT death FROM killdeath WHERE player=?", sourcename)
               deathsplayer = deathdatalesen[1].kill
 
 
end     
end
 
addEventHandler ( "onPlayerJoin", getRootElement(), onjoindbkill )

Add data from db in Scoreboard:

local function resetScores (element)
setScoreData(element, "Kills", killsplayer)
setScoreData(element, "Deaths", deathsplayer)
setScoreData(element, "Self", deathsplayer)
setScoreData(element, "Ratio", "-")
setScoreData(element, "Money", 0)
local status = ""
if isPedDead(element) then
	status = "Dead"
end
setScoreData(element, "status", status)
end

Kill and death Counter:

addEventHandler("onPlayerWasted", root,
function (ammo, killer, weapon)
if killer then
if killer ~= source then
-- killer killed victim
			setScoreData(killer, "Kills", getElementData(killer, "Kills") + 1)
			setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1)
if isColumnActive["ratio"] then
				updateRatio(killer)
				updateRatio(source)
end
else
-- victim killed himself
			setScoreData(source, "self", getElementData(source, "self") + 1)
end
else
-- victim died
		setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1)
if isColumnActive["ratio"] then
			updateRatio(source)
end
end
 
	setScoreData(source, "status", "Dead")
end
)

Save Data when Player leave:

function onquitdbkill ()
local sourcename = getPlayerName ( source )
       enddaths = getElementData(source, "Deaths")
       endkills = getElementData(source, "Kills")
executeSQLUpdate ( "killdeath", "kill = '" .. endkills .. "', death = '" .. endkills .. "'", "player = '" .. sourcename .. "'" )
end
addEventHandler ( "onPlayerQuit", getRootElement(), onquitdbkill )

And here are my full script:

-- Create a new Data
 
function dbaddkilldeath ()
executeSQLCreateTable ( "killdeath", "kill INTEGER, death INTEGER, player TEXT" )
end
addCommandHandler ("dbaddkill", dbaddkilldeath)
 
 
-- Read the Data
 
function onjoindbkill (player) 
local sourcename = getPlayerName (source) 
       killdatalesen = executeSQLQuery("SELECT kill FROM killdeath WHERE player=?", sourcename)
if(#player == 0) then  
executeSQLInsert ( "killdeath", "'1', '1', '" .. sourcename .. "'" )
else
               killsplayer = killdatalesen[1].kill
 
               deathdatalesen = executeSQLQuery("SELECT death FROM killdeath WHERE player=?", sourcename)
               deathsplayer = deathdatalesen[1].kill
 
 
end     
end
 
addEventHandler ( "onPlayerJoin", getRootElement(), onjoindbkill )
 
 
local root = getRootElement()
local scoresRoot = getResourceRootElement(getThisResource())
local scoreboard = getResourceFromName("scoreboard")
 
local scoreColumns = {"Kills", "Deaths", "Self", "Ratio", "Status", "Money"}
local isColumnActive = {}
 
local KDR_DECIMAL_PLACES = 2
 
--http://lua-users.org/wiki/SimpleRound
local function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
 
local function setScoreData (element, column, data)
if isColumnActive[column] then
setElementData(element, column, data)
end
end
 
 
local function resetScores (element)
setScoreData(element, "Kills", killsplayer)
setScoreData(element, "Deaths", deathsplayer)
setScoreData(element, "Self", deathsplayer)
setScoreData(element, "Ratio", "-")
setScoreData(element, "Money", 0)
local status = ""
if isPedDead(element) then
	status = "Dead"
end
setScoreData(element, "status", status)
end
 
function trans (player)
 
setElementData(player,"Money",tostring(getPlayerMoney(player)))
 
end
 
 
addEventHandler("onPlayerWasted", getRootElement(), 
function (player)
if player then
setElementData(source,"Money",getPlayerMoney(source))  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
end
end
)
 
addEventHandler("onPlayerLogin", getRootElement(), 
function (player)
if player then
setElementData(source,"Money",getPlayerMoney(source))  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
end
end
)
 
addEventHandler("onPlayerVehicleEnter", getRootElement(), 
function (player)
if player then
setElementData(source,"Money",getPlayerMoney(source))  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
end
end
)
 
 
addEventHandler("onPlayerVehicleExit", getRootElement(), 
function (player)
if player then
setElementData(source,"Money",getPlayerMoney(source))  -- Set the player's "Money" data to his current money amount. This will automatically update the scoreboard column
end
end
)
 
 
local function updateRatio (element)
local Deaths = getElementData(element, "Deaths")
if Deaths == 0 then
	setScoreData(element, "Ratio", "-")
else
local kdr = round(getElementData(element, "Kills") / Deaths, KDR_DECIMAL_PLACES)
	setScoreData(element, "ratio", tostring(kdr))
end
end
 
function updateActiveColumns ()
for i, column in ipairs(scoreColumns) do
if get(column) then
		isColumnActive[column] = true
call(scoreboard, "addScoreboardColumn", column)
elseif isColumnActive[column] then
		isColumnActive[column] = false
call(scoreboard, "removeScoreboardColumn", column)
end
end
end
 
addEventHandler("onResourceStart", scoresRoot,
function ()
	updateActiveColumns()
for i, player in ipairs(getElementsByType("player")) do
		resetScores(player)
end
end
)
 
addEventHandler("onResourceStop", scoresRoot,
function ()
for i, column in ipairs(scoreColumns) do
if isColumnActive[column] then
call(scoreboard, "removeScoreboardColumn", column)
end
end
end
)
 
addEventHandler("onPlayerJoin", root,
function ()
	resetScores(source)
end
)
 
addEventHandler("onPlayerWasted", root,
function (ammo, killer, weapon)
if killer then
if killer ~= source then
-- killer killed victim
			setScoreData(killer, "Kills", getElementData(killer, "Kills") + 1)
			setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1)
if isColumnActive["ratio"] then
				updateRatio(killer)
				updateRatio(source)
end
else
-- victim killed himself
			setScoreData(source, "self", getElementData(source, "self") + 1)
end
else
-- victim died
		setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1)
if isColumnActive["ratio"] then
			updateRatio(source)
end
end
 
	setScoreData(source, "status", "Dead")
end
)
 
addEventHandler("onPlayerSpawn", root,
function ()
	setScoreData(source, "status", "")
end
)
 
addCommandHandler("score",
function (player)
if player then
for i, column in ipairs(scoreColumns) do
if column == "status" then
break
end
if isColumnActive[column] then
call(scoreboard, "addScoreboardColumn", column)
outputConsole(column .. ": " .. getElementData(player, column), player)
end
end
end
end
)
 
 
-- Data Save after logout
 
function onquitdbkill ()
local sourcename = getPlayerName ( source )
       enddaths = getElementData(source, "Deaths")
       endkills = getElementData(source, "Kills")
executeSQLUpdate ( "killdeath", "kill = '" .. endkills .. "', death = '" .. endkills .. "'", "player = '" .. sourcename .. "'" )
end
addEventHandler ( "onPlayerQuit", getRootElement(), onquitdbkill )

I have create a the db that is not the problem, I hope someone can help me there too :D THX!!!!!!!!!!!

Link to comment

In the event onPlayerJoin you use the variable player, that variable aint exists. Use source. The same gous for onPlayerQuit!

https://wiki.multitheftauto.com/index.ph ... PlayerJoin

https://wiki.multitheftauto.com/index.ph ... PlayerQuit

Some very important pages on the wiki:

https://wiki.multitheftauto.com/index.ph ... =Main_Page

https://wiki.multitheftauto.com/index.ph ... _Functions

https://wiki.multitheftauto.com/index.ph ... ing_Events

https://wiki.multitheftauto.com/index.ph ... _Functions

https://wiki.multitheftauto.com/index.ph ... ing_Events

Your code is very good explained on the wiki, please first search stuff on the wiki, and then if it wont work, ask here :)

Link to comment

ok i have test now that for update the money:

function updatemoneypl (player)
if player then
setTimer (setElementData, 5000, 0, source,"Money",getPlayerMoney(source))
-- setElementData(source,"Money",getPlayerMoney(source))
end
end
addEventHandler ( "onResourceStart", getRootElement(), updatemoneypl )

But nothing no error nothing :?:

then i have test that:

function updatemoneypl (player)
if player then
-- setTimer (setElementData, 5000, 0, source,"Money",getPlayerMoney(source))
setElementData(source,"Money",getPlayerMoney(source))
end
end
setTimer ( updatemoneypl, 5000, 0 )
addEventHandler ( "onResourceStart", getRootElement(), updatemoneypl )

Nothing too :?

Can someone help me plz? THX :D

Link to comment
function updatemoneypl (player)
...
addEventHandler ( "onResourceStart", getRootElement(), updatemoneypl )

I've always been wondering this:

Where the heck do ppl get the "player" from at onResourceStart? D:

Just so you know, onResourceStart doesn't have a player argument, and therefore doesn't have the capability to work in the way you've given. I don't really see what you want to do either, perhaps if you know what you need you can find an event which is suited for your intended use.

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