lehmartins
New Member
- Joined
- Jul 20, 2016
- Messages
- 13
- Reaction score
- 1
data/scripts
as a .lua
file.local fromPosition = Position(1017, 1111, 7) -- top-left corner
local toPosition = Position(1092, 1736, 7) -- bottom-right corner
local manaRestoreAmount = 10
local globalevent = GlobalEvent("example")
function globalevent.onThink(...)
for _, player in ipairs(Game.getPlayers()) do
if player:getPosition():isInRange(fromPosition, toPosition) then
player:addMana(manaRestoreAmount)
end
end
return true
end
globalevent:interval(2000) -- interval in milliseconds
globalevent:register()
YOU IS GREAT!!!Place intodata/scripts
as a.lua
file.
LUA:local fromPosition = Position(1017, 1111, 7) -- top-left corner local toPosition = Position(1092, 1736, 7) -- bottom-right corner local manaRestoreAmount = 10 local globalevent = GlobalEvent("example") function globalevent.onThink(...) for _, player in ipairs(Game.getPlayers()) do if player:getPosition():isInRange(fromPosition, toPosition) then player:addMana(manaRestoreAmount) end end return true end globalevent:interval(2000) -- interval in milliseconds globalevent:register()
Would it be in these files? Trainer entrance and exit in movements? Can you help me with the code? I have no idea.Escanear toda a zona de treinamento é uma ideia péssima e custa muito desempenho (USO DE CPU),
você deve armazenar o ID do jogador na mesa quando ele entrar nesta zona e expulsá-lo da mesa se ele sair da zona.
I understood it in theory, but I couldn't apply it, can you help me? I would be very grateful!make it non-logout tile, add players to lua table when they enter the teleport, when they leave trainers remove from lua table, addevent every 2 seconds to give 10 mana only if in lua table
requires onStepIn
(check AID to enter -> add to lua table)
(checkAID to exit -> remove from lua table)
requires addEvent
once first player enters, initiate addEvent every 2 seconds, give them mana)
local nonLogoutPlayers = {}
-- Function to give mana to players in the non-logout zone
local function giveManaToPlayers()
for playerId, _ in pairs(nonLogoutPlayers) do
local player = Player(playerId)
if player then
player:addMana(10)
player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE) -- Optional: visual effect
else
-- Remove players who are no longer online
nonLogoutPlayers[playerId] = nil
end
end
-- Continue the event if there are still players in the zone
if next(nonLogoutPlayers) then
addEvent(giveManaToPlayers, 2000) -- Repeat every 2 seconds
end
end
-- Function to start the mana regeneration when the first player enters
local function startManaRegen()
if not next(nonLogoutPlayers) then
addEvent(giveManaToPlayers, 2000) -- Start mana regeneration
end
end
-- Event for when a player steps in the teleport (revscriptsys format)
function onStepIn(player, item, position, fromPosition)
if not player or not player:isPlayer() then
return false
end
local aid = item:getActionId()
-- Check if it's the teleport AID for entering the non-logout zone
if aid == 10001 then -- Replace with your teleport's AID for entering
if not nonLogoutPlayers[player:getId()] then
nonLogoutPlayers[player:getId()] = true
player:sendTextMessage(MESSAGE_INFO_DESCR, "You have entered the non-logout zone.")
startManaRegen() -- Start mana regen if it's the first player entering
end
elseif aid == 10002 then -- Replace with your teleport's AID for exiting
if nonLogoutPlayers[player:getId()] then
nonLogoutPlayers[player:getId()] = nil
player:sendTextMessage(MESSAGE_INFO_DESCR, "You have left the non-logout zone.")
end
end
return true
end
-- Register the event for step-in actions
local teleportStepIn = TalkAction("onStepIn")
teleportStepIn:id(10001) -- Set the appropriate AID for entering the teleport
teleportStepIn:id(10002) -- Set the appropriate AID for exiting the teleport
teleportStepIn:register()
My script doesn't scan the entire area. It just loops through all the online players and checks if their position is inside the area.Scan entire training zone its absolute bad idea and cost a lot of performance (CPU USAGE),
you should store player's id into the table when they enter to this zone, and kick them from the table if they exit the zone.
If you want to go this route, then I'd use an onThink event. register onStepIn. unregister onStepOut.make it non-logout tile, add players to lua table when they enter the teleport, when they leave trainers remove from lua table, addevent every 2 seconds to give 10 mana only if in lua table
requires onStepIn
(check AID to enter -> add to lua table)
(checkAID to exit -> remove from lua table)
requires addEvent
once first player enters, initiate addEvent every 2 seconds, give them mana)
local manaRestoreAmount = 10
local tile_actionId = 45001
local creatureevent = CreatureEvent("onThink_trainingMana")
function creatureevent.onThink(creature, interval)
creature:addMana(manaRestoreAmount)
return true
end
creatureevent:register()
------------
local moveevent = MoveEvent()
function moveevent.onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
return true
end
creature:registerEvent("onThink_trainingMana")
return true
end
moveevent:aid(tile_actionId)
moveevent:register()
------------
local moveevent = MoveEvent()
function moveevent.onStepOut(creature, item, position, fromPosition)
if not creature:isPlayer() then
return true
end
creature:unregisterEvent("onThink_trainingMana")
return true
end
moveevent:aid(tile_actionId)
moveevent:register()
Less code, cleaner, so much cleaner, seems more straight forward. I think this is the way to go. Good job @XikiniMy script doesn't scan the entire area. It just loops through all the online players and checks if their position is inside the area.
If you want to go this route, then I'd use an onThink event. register onStepIn. unregister onStepOut.
Put the actionId on every training tile.
LUA:local manaRestoreAmount = 10 local tile_actionId = 45001 local creatureevent = CreatureEvent("onThink_trainingMana") function creatureevent.onThink(creature, interval) creature:addMana(manaRestoreAmount) return true end creatureevent:register() ------------ local moveevent = MoveEvent() function moveevent.onStepIn(creature, item, position, fromPosition) if not creature:isPlayer() then return true end creature:registerEvent("onThink_trainingMana") return true end moveevent:aid(tile_actionId) moveevent:register() ------------ local moveevent = MoveEvent() function moveevent.onStepOut(creature, item, position, fromPosition) if not creature:isPlayer() then return true end creature:unregisterEvent("onThink_trainingMana") return true end moveevent:aid(tile_actionId) moveevent:register()