• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Check other members of party

Obito

0x1337
Joined
Feb 17, 2011
Messages
351
Solutions
8
Reaction score
152
Location
Egypt
Good day :),
I'm working on a script for a game that involves casting a spell at a party. The idea is that the spell can only be cast if all party members are within a certain distance of the caster.

This my code:
Lua:
local DISTANCE_SQM = 2

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
combat:setParameter(COMBAT_PARAM_DISPEL, CONDITION_INVISIBLE)
combat:setArea(createCombatArea(AREA_CIRCLE3X3))

function onCastSpell(creature, variant)
   local party = creature:getParty()
   if not party then
      creature:sendCancelMessage("You are not in a party.")
      return false
   end
   local partyMembers = party:getMembers()
   if #partyMembers == 0 then
      creature:sendCancelMessage("Your party has no members.")
      return false
   end

   for i, member in ipairs(partyMembers) do
      if member:getPosition():getDistance(creature:getPosition()) > DISTANCE_SQM then
         creature:sendCancelMessage("Not all party members are within "..DISTANCE_SQM.." sqm.")
         return false
      end
   end

   return combat:execute(creature, variant)
end
The problem I'm having is that even though the code checks that all party members are within the specified distance before casting the spell, invited players can cast the spell from any area without any checks and the code works in the leader only.
I'm not sure why this is happening, and I would appreciate any help or insight anyone can offer. Thank you!
 
Solution
The problem is that leader is not returned by getMembers that's why it does not work when you test it.
Try:
Lua:
function onCastSpell(creature, variant)
   local party = creature:getParty()
   if not party then
      creature:sendCancelMessage("You are not in a party.")
      return false
   end

   local partyMembers = party:getMembers()
   if #partyMembers == 0 then
      creature:sendCancelMessage("Your party has no members.")
      return false
   end

   local leader = party:getLeader()
   if creature:getId() ~= leader:getId() then
      table.insert(partyMembers, leader)
   end

   local position = creature:getPosition()
   for _, member in pairs(partyMembers) do
      local memberPosition = member:getPosition()...
There is party:getInvitees() as well.

So, presumably members are the leader + party member + invitee?

You'd have to print out some information to console and check..

But if that is the case, then you can remove invitee's from the member count, to get an accurate number of members in the party..
and when looping through the members, check if they are in the invitee group.. and omit them from the distance check.
And of course you check if the person casting is an invitee.. and return false.

Couple more functions that might be helpful in there.

I also vaguely remember that the leader is a special case, where you had to almost check them separately from the other members..
But that might've just been part of the script I was writing at the time?

Can't really help more on this one, since I'd need a server up and running to test all the functions properly.

If you're still having trouble, add a bunch of prints of different information regarding the party, and reply back here.
The extra information can give us a better clue as to the actual issue.
 
The problem is that leader is not returned by getMembers that's why it does not work when you test it.
Try:
Lua:
function onCastSpell(creature, variant)
   local party = creature:getParty()
   if not party then
      creature:sendCancelMessage("You are not in a party.")
      return false
   end

   local partyMembers = party:getMembers()
   if #partyMembers == 0 then
      creature:sendCancelMessage("Your party has no members.")
      return false
   end

   local leader = party:getLeader()
   if creature:getId() ~= leader:getId() then
      table.insert(partyMembers, leader)
   end

   local position = creature:getPosition()
   for _, member in pairs(partyMembers) do
      local memberPosition = member:getPosition()
      local distance = position:getDistance(memberPosition)
      if distance > DISTANCE_SQM then
         creature:sendCancelMessage("Not all party members are within "..DISTANCE_SQM.." sqm.")
         return false
      end
   end

   return combat:execute(creature, variant)
end
In addition you could also not check distance for "yourself" as for non-leader players it also check distance (distance always zero).
 
Last edited:
Solution
The problem is that leader is not returned by getMembers that's why it does not work when you test it.
Try:
Lua:
function onCastSpell(creature, variant)
   local party = creature:getParty()
   if not party then
      creature:sendCancelMessage("You are not in a party.")
      return false
   end

   local partyMembers = party:getMembers()
   if #partyMembers == 0 then
      creature:sendCancelMessage("Your party has no members.")
      return false
   end

   local leader = party:getLeader()
   if creature:getId() ~= leader:getId() then
      table.insert(partyMembers, leader)
   end

   local position = creature:getPosition()
   for _, member in pairs(partyMembers) do
      local memberPosition = member:getPosition()
      local distance = position:getDistance(memberPosition)
      if distance > DISTANCE_SQM then
         creature:sendCancelMessage("Not all party members are within "..DISTANCE_SQM.." sqm.")
         return false
      end
   end

   return combat:execute(creature, variant)
end
In addition you could also not check distance for "yourself" as for non-leader players it also check distance (distance always zero).
Your suggestion to check if the casting creature is the party leader and include them in the list of party members if they are has been incredibly helpful and it works fine thanks so much @lursky.
There is party:getInvitees() as well.

So, presumably members are the leader + party member + invitee?

You'd have to print out some information to console and check..

But if that is the case, then you can remove invitee's from the member count, to get an accurate number of members in the party..
and when looping through the members, check if they are in the invitee group.. and omit them from the distance check.
And of course you check if the person casting is an invitee.. and return false.

Couple more functions that might be helpful in there.

I also vaguely remember that the leader is a special case, where you had to almost check them separately from the other members..
But that might've just been part of the script I was writing at the time?

Can't really help more on this one, since I'd need a server up and running to test all the functions properly.

If you're still having trouble, add a bunch of prints of different information regarding the party, and reply back here.
The extra information can give us a better clue as to the actual issue.
Thank you both for your helpful suggestions! @lurky's solution seems to be working for me at the moment, but I appreciate your idea as well. Unfortunately, my lack of experience with Lua scripting makes it a bit challenging for me to implement your suggestion right now. However, I am definitely open to exploring it in the future as I continue to learn and grow in my scripting skills. Once again, thank you both @Xikini @lursky for your expertise and willingness to help. It's greatly appreciated!
 
Back
Top