Okay, I'll post the code with comments so hopefully you will learn how to do something similar (or modificiations to it) by yourself:
Make sure that global storage values 31313 and 31314 are not used anywhere else on the server already, or after this.
If you don't know what i'm talking about, there's a high chance that it's not used then, so we're good to go.
If they are used, then alter my code and change occurrences of 31313 and 31314 to some numbers that are not used.
In
lib/050-functions.lua, a library for functions, we will create 2 new functions:
Code:
--- FUNCTIONS LIB:
function triggerHuntFor(cid)
if getGlobalStorageValue(31314) ~= cid then -- Read the comment below first -> We won't do that if the highest level is already this same person.
setGlobalStorageValue(31314, cid) -- We set a global storage value to be equal to the input userdata. This will give us a global variable that we can quickly access and/or change to indicate which player is the highest level atm online.
setGlobalStorageValue(31313, getPlayerLevel(cid)) -- We set a global storage value to be equal to the input userdata. This will give us a global variable that we can quickly access and/or change to indicate which player is the highest level atm online.
doBroadcastMessage("[The Hunt is On!]\nPlayer " ..getCreatureName(cid).. " is the highest level online and thus hunted.\nKilling him/her will reward a bounty!") -- Broadcast message
end
end
-- A function that will return the user data of the highest level player currently online in the game
function getHighestOnlineLevel(self,skipself)
local highest = {id = 0, level = 0} -- We set an array that will hold some data for us. By default, this data is equal to 0.
for _, name in ipairs(getOnlinePlayers()) do -- We initiate a loop that will go through every online player (Warning: This can be resource intensive, especially if repeated at a frequent interval. Do not make the interval too low.
local cid = getPlayerByName(name) -- We get the userdata of the player that's currently being checked
if cid == self and skipself == true then
-- Nothing goes on this line. We skip doing anything here, because we deliberately set the parameters to skip this player in the search
else
if getPlayerLevel(cid) > highest.level then -- If the level of the player that's currently being checked is higher than the highest level we detected so far, we set the highest.level value to be his level, and store his id in highest.id
highest.level = getPlayerLevel(cid)
highest.id = cid
end
end
end
return highest.id -- The function returns us the user data of the player with the highest level, then we can do something with it later
end
In creaturescripts.xml, we'll add these new scripts:
logoutbh.lua
Code:
function onLogout(cid)
-- When you log out, if you were the highest level, then we search for another player who is the highest level after you log out
if getGlobalStorageValue(31314) == cid then
local person = getHighestOnlineLevel(cid, true) -- Get the highest online level (excluding yourself, since you're about to log out) and store that player's userdata in variable 'person'
doBroadcastMessage("[The Hunt is On!]\nPlayer " ..getPlayerName(cid).. " has logged out as the highest level on the server.\nA new player has been marked for the bounty: " ..getCreatureName(person).. "!\nGet him/her!") -- Broadcast message
triggerHuntFor(cid)
end
return true
end
killbh.lua
Code:
function onKill(cid, target)
if isPlayer(cid) and isPlayer(target) then
if getGlobalStorageValue(31314) == target then
doBroadcastMessage("[The Hunt is On!]\nPlayer " ..getCreatureName(cid).. " killed the hunter player " ..getCreatureName(target).. " and claimed the bounty!") -- Broadcast message
doPlayerAddItem(cid, rewardID, stacks) -- EDIT rewardID and stacks to your liking. Copy/paste this line if you want to give more than just 1 item.
end
end
return true
end
Now, open creaturescripts/scripts/
login.lua and add this to it in an appropriate place:
Code:
registerCreatureEvent(cid, "LogoutBH") -- Registers a custom creature event that we made to the player (so that the script may be loaded for that player)
registerCreatureEvent(cid, "KillBH") -- Registers a custom creature event that we made to the player (so that the script may be loaded for that player)
if getPlayerLevel(cid) > getGlobalStorageValue(31313) then -- When you log in, if your level is higher than the currently highest level, you become the new hunted target
triggerHuntFor(cid)
doBroadcastMessage("[The Hunt is On!]\nPlayer " ..getPlayerName(cid).. " has logged in as the highest level on the server.\nThe bounty is on his/her head!") -- Broadcast message
end
And finally, open
creaturescripts.xml and add this:
Code:
<event type="kill" name="KillBH" event="script" value="killbh.lua"/>
<event type="logout" name="LogoutBH" event="script" value="logoutbh.lua"/>
Make sure to alter the doPlayerAddItem function in killbh.lua to give the reward you want it to give.
You will find that such a system in its current state is faulty, by the way (but this is exactly what you requested, so I'm giving you what you requested. You might need to think about more details next time you request a script).
Reasons:
It may spam the broadcast messages whenever someone logs in/logs out if they happen to be the highest level.
It may lag the server, depending on the PC's performance, if the getHighestOnlineLevel(self,skipself) function is called too frequently or a lot at a very small time period. (Which would happen if a ton of players that are hunted repeatedly log out)
Hope it helps out.
EDIT: Btw, I didn't test this, just wrote it, so you might wanna test it out first and post if there are some issues. I may have made some mistake, idk, too lazy to re-check everything again
EDIT 2: You may also want to rename the thread (if that's possible, i think it should be) to something more indicative of its content, so that the people using the
Search function on OTLand can find proper results easier.