GTAGaming Forums

GTAGaming Forums (http://www.gtagaming.com/forums/index.php)
-   GTA III, Vice City, and San Andreas Mods (http://www.gtagaming.com/forums/forumdisplay.php?f=100)
-   -   stats (http://www.gtagaming.com/forums/showthread.php?t=124839)

[TTH]BiZkiT 10-19-2010 11:10 AM

stats
 
Hello, i have a problem with a stats script i want to use with VLE but how can i get this to work without having problems with onPlayerConnect and onPlayerDeath functions VLE uses.
This is the stats script:
If someone could help me out would be nice:)

--------------------------------------------------------------------------------
-- Statistics of Players
--------------------------------------------------------------------------------

local Kills = {}
local Deaths = {}

function onPlayerConnect(PlayerID)
IonPlayerConnect(PlayerID)
givePlayerCash(PlayerID,5000)
Kills[strval(PlayerID)] = 0
Deaths[strval(PlayerID)] = 0

-- Random colors upon connection
setPlayerColor(PlayerID,math.random(0,255),math.ra ndom(0,255),math.random(0,255))

-- Connect messages
outputChatBox(PlayerID,"Welcome to the server, " .. getPlayerName(PlayerID) .. "!",0,255,0,255)
outputChatBox(PlayerID,"BE SURE TO TYPE \"/commands\" TO VIEW THE SERVER'S COMMANDS!",255,255,0,255)
end

function onPlayerDeath(PlayerID,KillerID,WeaponID)
IonPlayerDeath(PlayerID,KillerID,WeaponID)
Deaths[PlayerID] = Deaths[PlayerID] + 1

-- Case for self-kill
if strval(KillerID) == 255 then
if WeaponID == 255 then
outputChatBox(getPlayerName(strval(PlayerID)) .. " died.",255,255,0,255)
else
outputChatBox(getPlayerName(strval(PlayerID)) .. " died. (" .. getWeaponName(strval(WeaponID)) .. ")",255,255,0,255)
end
return
end
outputChatBox(getPlayerName(strval(KillerID)) .. " killed " .. getPlayerName(strval(PlayerID)) .. ". (" .. getWeaponName(strval(WeaponID)) .. ")",255,255,0,255)

-- Increase the score
local Score = strval(getPlayerScore(strval(KillerID)))
setPlayerScore(KillerID,Score + 1)
Kills[KillerID] = Kills[KillerID] + 1

-- Give the other player money
if getPlayerCash(PlayerID) > 500 then
givePlayerCash(PlayerID,-DEATH_MONEY)
outputChatBox(KillerID,"Congratulations! You have obtained $" .. DEATH_MONEY .. " from your ace kill!",0,255,0,255)
givePlayerCash(KillerID,DEATH_MONEY)
else
if getPlayerCash(PlayerID) == 0 then
outputChatBox(KillerID,"Your opponent is broke and thus you have not received any money. :-(",0,255,0,255)
else
outputChatBox(KillerID,"Congratulations! You have obtained $" .. getPlayerCash(PlayerID) .. " from your ace kill!",0,255,0,255)
end
givePlayerCash(KillerID,getPlayerCash(PlayerID))
setPlayerCash(PlayerID,0)
end
end

function OutputStats(PlayerID,OptionalOtherPlayer)
if OptionalOtherPlayer == nil then -- They entered nothing for the parameter
local Ratio = 0
if Deaths[PlayerID] == 0 then
if Kills[PlayerID] == 0 then
Ratio = 1
else
Ratio = Kills[PlayerID]
end
else
Ratio = (((Kills[PlayerID]) / (Deaths[PlayerID])) / 100.0) * 100
end
outputChatBox(PlayerID, "Your stats are: [Kills: " .. Kills[PlayerID] .. " || Deaths: " .. Deaths[PlayerID] .. " || Ratio: " .. Ratio .. "]",255,255,0,255)
else
local Player = strval(OptionalOtherPlayer)
if Deaths[PlayerID] == 0 then
if Kills[Player] == 0 then
Ratio = 1
else
Ratio = Kills[Player]
end
else
Ratio = (((Kills[Player]) / (Deaths[Player])) / 100.0) * 100
end
outputChatBox(PlayerID, getPlayerName(Player) .. "'s stats are: [Kills: " .. Kills[Player] .. " || Deaths: " .. Deaths[Player] .. " || Ratio: " .. Ratio .. "]",255,255,0,255)
end
end

addCommand("stats","OutputStats")

function TopScore(PlayerID)
local TopPlayerKillID = -1
local TopPlayerRatioID = -1
local TopKills = 0
local TopRatio = 0

-- Loop through all of the players
for i = 0, 100 do
if isPlayerConnected(i) == 1 then
local Player = strval(i)
local Ratio = 0
if Deaths[Player] == 0 then
if Kills[Player] == 0 then
Ratio = 1
else
Ratio = Kills[Player]
end
else
Ratio = (((Kills[Player]) / (Deaths[Player])) / 100.0) * 100
end
if Ratio > TopRatio then
TopPlayerRatioID = Player
TopRatio = Ratio
end
if Kills[Player] > TopKills then
TopKills = Kills[Player]
TopPlayerKillID = Player
end
end
end

-- Double check
for i = 0, 100 do
if isPlayerConnected(i) == 1 then
local Player = strval(i)
local Ratio = 0
if Deaths[Player] == 0 then
if Kills[Player] == 0 then
Ratio = 1
else
Ratio = Kills[Player]
end
else
Ratio = (((Kills[Player]) / (Deaths[Player])) / 100.0) * 100
end
local Found = false
if Ratio == TopRatio and Player ~= TopPlayerRatioID then
TopPlayerRatioID = -1
Found = true
end
if Kills[Player] == TopKills and Player ~= TopPlayerKillID then
TopPlayerKillID = -1
Found = true
end
if Found == true then break end
end
end

-- Now output
local TopKillName = "N/A"
local TopNameRatio = "N/A"

if TopPlayerKillID > -1 then TopKillName = getPlayerName(strval(TopPlayerKillID)) end
if TopPlayerRatioID > -1 then TopNameRatio = getPlayerName(strval(TopPlayerRatioID)) end

outputChatBox(PlayerID,"Leaderboard: [Top Kills: " .. TopKillName .. " || Top Ratio: " .. TopNameRatio .. "]",255,255,0,255)
end

addCommand("top","TopScore")

Mex 10-21-2010 10:18 AM

All vco callback function names are taken by vle, you can bind a function to a vle/vco callback with for example:

Code:

vaddCallback("onPlayerConnect", "onPlayerConnect_stats")
function onPlayerConnect_stats(PlayerID)
  -- callback code
end

more info on the vle callback functions here: http://wiki.gtagaming.com/VLE_Callbacks
and example of vaddCallback() used in VLE, on lines 35 to 43 here: http://vle.vicecityonline.com/index....e=code&file=79

[TTH]BiZkiT 10-25-2010 03:54 PM

been playing with this but there must be something wrong im doing.
this is what i tried can you tell me what i do wrong? server crashes on connect

local Kills = {}
local Deaths = {}
vaddCallback("onPlayerConnect", "onPlayerConnect_stats")
function onPlayerConnect_stats(PlayerID)
-- callback
IonPlayerConnect(PlayerID)
givePlayerCash(PlayerID,5000)
Kills[strval(PlayerID)] = 0
Deaths[strval(PlayerID)] = 0

-- Random colors upon connection
setPlayerColor(PlayerID,math.random(0,255),math.ra ndom(0,255),math.random(0,255))

-- Connect messages
outputChatBox(PlayerID,"BE SURE TO TYPE "/cmds" TO VIEW THE SERVER'S COMMANDS!",257,23,13,255)
outputChatBox(PlayerID,"BE SURE TO TYPE "/teles" TO VIEW THE TELEPORT COMMANDS!",252,209,22,255)
outputChatBox(PlayerID,"This server has custom maps running!",252,209,22,255)
end

vaddCallback("onPlayerDeath", "onPlayerDeath_stats")
function onPlayerDeath_stats(PlayerID,KillerID,WeaponID)
-- callback
IonPlayerDeath(PlayerID,KillerID,WeaponID)
Deaths[PlayerID] = Deaths[PlayerID] + 1

-- Case for self-kill
if strval(KillerID) == 255 then
if WeaponID == 255 then
outputChatBox(getPlayerName(strval(PlayerID)) .. " died.",255,255,0,255)
else
outputChatBox(getPlayerName(strval(PlayerID)) .. " died. (" .. getWeaponName(strval(WeaponID)) .. ")",255,255,0,255)
end
return
end

Mex 10-26-2010 01:21 PM

in the setPlayerColor() function args, math.ra ndom(0,255) shouldn't have a space after the a

also in the first 2 connect messages you've used quotation marks inside the echo text, but the text is surrounded by them, so Lua will think the strings end after the word TYPE.
you need to escape the quotation marks used inside the strings with a backslash before them, or use other characters to define the strings:

outputChatBox(PlayerID,"BE SURE TO TYPE \"/cmds\" TO VIEW THE SERVER'S COMMANDS!",257,23,13,255)
outputChatBox(PlayerID,"BE SURE TO TYPE \"/teles\" TO VIEW THE TELEPORT COMMANDS!",252,209,22,255)

outputChatBox(PlayerID,'BE SURE TO TYPE "/cmds" TO VIEW THE SERVER'S COMMANDS!',257,23,13,255)
outputChatBox(PlayerID,[[BE SURE TO TYPE "/teles" TO VIEW THE TELEPORT COMMANDS!]],252,209,22,255)

[TTH]BiZkiT 10-28-2010 01:42 PM

fixed both isues still it doesnt seem to work:( as soon i have the script loaded and i enter the server it crashes.
In server console there no errors.
This is the whole script so far --
--------------------------------------------------------------------------------
-- Stats
--------------------------------------------------------------------------------

local Kills = {}
local Deaths = {}
vaddCallback("onPlayerConnect", "onPlayerConnect_beer")
function onPlayerConnect_beer(PlayerID)
-- callback
onPlayerConnect(PlayerID)
givePlayerCash(PlayerID,5000)
Kills[strval(PlayerID)] = 0
Deaths[strval(PlayerID)] = 0

-- Random colors upon connection
setPlayerColor(PlayerID,math.random(0,255),math.ra ndom(0,255),math.random(0,255))

-- Connect messages
outputChatBox(PlayerID,'BE SURE TO TYPE "/cmds" TO VIEW THE SERVER COMMANDS!',257,23,13,255)
outputChatBox(PlayerID,'BE SURE TO TYPE "/teles" TO VIEW THE TELEPORT COMMANDS!',252,209,22,255)
end

vaddCallback("onPlayerDeath", "onPlayerDeath_beer")
function onPlayerDeath_beer(PlayerID,KillerID,WeaponID)
-- callback
onPlayerDeath(PlayerID,KillerID,WeaponID)
Deaths[PlayerID] = Deaths[PlayerID] + 1

-- Case for self-kill
if strval(KillerID) == 255 then
if WeaponID == 255 then
outputChatBox(getPlayerName(strval(PlayerID)) .. " died.",255,255,0,255)
else
outputChatBox(getPlayerName(strval(PlayerID)) .. " died. (" .. getWeaponName(strval(WeaponID)) .. ")",255,255,0,255)
end
return
end
outputChatBox(getPlayerName(strval(KillerID)) .. " killed " .. getPlayerName(strval(PlayerID)) .. ". (" .. getWeaponName(strval(WeaponID)) .. ")",255,255,0,255)

-- Increase the score
local Score = strval(getPlayerScore(strval(KillerID)))
setPlayerScore(KillerID,Score + 1)
Kills[KillerID] = Kills[KillerID] + 1

-- Give the other player money
if getPlayerCash(PlayerID) > 500 then
givePlayerCash(PlayerID,-Bounty)
outputChatBox(KillerID,"GratZ! You have obtained $" .. BOUNTY .. " for your kill!",124,252,0,255)
givePlayerCash(KillerID,Bounty)
else
if getPlayerCash(PlayerID) == 0 then
outputChatBox(KillerID,"Your opponent is poor you have not received any money this time!. :-(",227,23,13,255)
else
outputChatBox(KillerID,"GratZ! You have obtained $" .. getPlayerCash(PlayerID) .. " for your kill!",124,252,0,255)
end
givePlayerCash(KillerID,getPlayerCash(PlayerID))
setPlayerCash(PlayerID,0)
end
end

function Stats(PlayerID,OptionalOtherPlayer)
if OptionalOtherPlayer == nil then -- They entered nothing for the parameter
local Ratio = 0
if Deaths[PlayerID] == 0 then
if Kills[PlayerID] == 0 then
Ratio = 1
else
Ratio = Kills[PlayerID]
end
else
Ratio = (((Kills[PlayerID]) / (Deaths[PlayerID])) / 100.0) * 100
end
outputChatBox(PlayerID, "Your stats are: [Kills: " .. Kills[PlayerID] .. " || Deaths: " .. Deaths[PlayerID] .. " || Ratio: " .. Ratio .. "]",255,255,0,255)
else
local Player = strval(OptionalOtherPlayer)
if Deaths[PlayerID] == 0 then
if Kills[Player] == 0 then
Ratio = 1
else
Ratio = Kills[Player]
end
else
Ratio = (((Kills[Player]) / (Deaths[Player])) / 100.0) * 100
end
outputChatBox(PlayerID, getPlayerName(Player) .. "'s stats are: [Kills: " .. Kills[Player] .. " || Deaths: " .. Deaths[Player] .. " || Ratio: " .. Ratio .. "]",255,255,0,255)
end
end

addCommand("stats","Stats")

function TopScore(PlayerID)
local TopPlayerKillID = -1
local TopPlayerRatioID = -1
local TopKills = 0
local TopRatio = 0

-- Loop through all of the players
for i = 0, 100 do
if isPlayerConnected(i) == 1 then
local Player = strval(i)
local Ratio = 0
if Deaths[Player] == 0 then
if Kills[Player] == 0 then
Ratio = 1
else
Ratio = Kills[Player]
end
else
Ratio = (((Kills[Player]) / (Deaths[Player])) / 100.0) * 100
end
if Ratio > TopRatio then
TopPlayerRatioID = Player
TopRatio = Ratio
end
if Kills[Player] > TopKills then
TopKills = Kills[Player]
TopPlayerKillID = Player
end
end
end

-- Double check
for i = 0, 100 do
if isPlayerConnected(i) == 1 then
local Player = strval(i)
local Ratio = 0
if Deaths[Player] == 0 then
if Kills[Player] == 0 then
Ratio = 1
else
Ratio = Kills[Player]
end
else
Ratio = (((Kills[Player]) / (Deaths[Player])) / 100.0) * 100
end
local Found = false
if Ratio == TopRatio and Player ~= TopPlayerRatioID then
TopPlayerRatioID = -1
Found = true
end
if Kills[Player] == TopKills and Player ~= TopPlayerKillID then
TopPlayerKillID = -1
Found = true
end
if Found == true then break end
end
end

-- Now output
local TopKillName = "N/A"
local TopNameRatio = "N/A"

if TopPlayerKillID > -1 then TopKillName = getPlayerName(strval(TopPlayerKillID)) end
if TopPlayerRatioID > -1 then TopNameRatio = getPlayerName(strval(TopPlayerRatioID)) end

outputChatBox(PlayerID,"Leaderboard: [Top Kills: " .. TopKillName .. " || Top Ratio: " .. TopNameRatio .. "]",255,255,0,255)
end

addCommand("top","TopScore")

oh and the space after the a comes if i copy it here:P its not in the script

Mex 10-28-2010 06:38 PM

This line:
Code:

vaddCallback("onPlayerConnect", "onPlayerConnect_beer")
tells VLE to call onPlayerConnect_beer() when onPlayerConnect() is called.

In function onPlayerConnect_beer(PlayerID) you've got:
Code:

onPlayerConnect(PlayerID)
Which will call onPlayerConnect(), and cause infinite function calls:

VCO calls onPlayerConnect()

onPlayerConnect() calls onPlayerConnect_beer()
onPlayerConnect_beer() calls onPlayerConnect()

So basically you shouldn't call onPlayerConnect() inside a function which gets called when onPlayerConnect() is called.

Same with: onPlayerDeath(PlayerID,KillerID,WeaponID)


All times are GMT -6. The time now is 04:45 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.