• 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 [TFS 0.3.6]Script Party Exp

Douglita

New Member
Joined
Nov 12, 2023
Messages
9
Reaction score
0
Hello everybody! I need help with a script...

I have this talkaction:

<talkaction words="!partyexp" event="script" value="partyexp.lua"/>

What calls this script:

Lua:
function onSay(cid, words, param, channel)
   if isInParty(cid) then
       local party = getPartyMembers(cid)
       local canEnableExpShare = true
       local sto = getPlayerStorageValue(cid, 4875498)

       for i = 1, #party do
           for j = i + 1, #party do
               local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
               if levelDifference > 15 then
                   canEnableExpShare = false
                   break
               end
           end
           if not canEnableExpShare then
               break
           end
       end

       if party[#party] == cid then
           if canEnableExpShare then
               for i = 1, #party do
                   if sto == 2 then
                       setPlayerStorageValue(party[i], 4875498, 1)
                       doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled!")
                   else
                       setPlayerStorageValue(party[i], 4875498, 2)
                       doPlayerSendTextMessage(party[i], 22, "The Exp in Party is enabled!")
                   end
               end
           else
               doPlayerSendTextMessage(cid, 27, "The level difference between party members cannot exceed 15 levels.")
               setPlayerStorageValue(cid, 4875498, 0)
           end
       else
           doPlayerSendTextMessage(cid, 27, "Only the leader of the party can do that!")
       end
   else
       doPlayerSendTextMessage(cid, 27, "You need to be a leader of a party to do that!")
   end
   return true         
end

This script will activate the sharing of experience in a party, this will only be possible if the level of all players in the party is not higher than 15 levels, both up and down. So far so good, the problem is that, for example, if there are two level 30 players in the party and they activate sharing, it will work. So, with sharing activated, if they invite a level 14 player, he will be able to receive the shared experience, because it was already activated before he joined.

I need to somehow make it so that every time a new player joins the party, sharing is automatically deactivated and I have to use the "!partyexp" command again, to check, or if anyone has any other ideas, to ensure that every time a new player joins the party, this check is carried out again to resolve this error.

Can anyone help me? Please!
 
Solution
Can you help me make this change to stop the infinite loop? @Xikini
Try this
Lua:
local function checkForPartyChanges(cid)
    if not isPlayer(cid) then
        return
    end
 
    if not isInParty(cid) then
        return
    end
 
    local sto = getPlayerStorageValue(cid, 4875498)
    local party = getPartyMembers(cid)
    local canEnableExpShare = true
 
    if sto == 1 then
        return
    end

    for i = 1, #party do
        for j = i + 1, #party do
            local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
            if levelDifference > 15 then
                canEnableExpShare = false
                break
            end
        end
        if not canEnableExpShare then...
You'd technically have the same issue if a level 15 & 30 were in a party, and the level 30 gained a level.

Your only real option is to have a looping check for the party experience share.

Lua:
local function experienceShareChecker(cid)
    if isInParty(cid) then
        local party = getPartyMembers(cid)
        local canEnableExpShare = true
        local sto = getPlayerStorageValue(cid, 4875498)
        
        for i = 1, #party do
            for j = i + 1, #party do
                local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
                if levelDifference > 15 then
                    canEnableExpShare = false
                    break
                end
            end
            if not canEnableExpShare then
                break
            end
        end
        
        if party[#party] == cid then
            if canEnableExpShare then
                for i = 1, #party do
                    if sto == 2 then
                        setPlayerStorageValue(party[i], 4875498, 1)
                        doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled!")
                    else
                        setPlayerStorageValue(party[i], 4875498, 2)
                        doPlayerSendTextMessage(party[i], 22, "The Exp in Party is enabled!")
                    end
                end
            else
                doPlayerSendTextMessage(cid, 27, "The level difference between party members cannot exceed 15 levels.")
                setPlayerStorageValue(cid, 4875498, 0)
            end
        else
            
        end
    else
        doPlayerSendTextMessage(cid, 27, "You need to be a leader of a party to do that!")
    end
    
    -- add a check here to stop the looping addEvent
    -- I'm unsure in what situations you'd want to stop the talkaction, otherwise I'd add it for you.
    addEvent(experienceShareChecker, 1000, cid)
end

function onSay(cid, words, param, channel)
    experienceShareChecker(cid)
    return true         
end
 
You'd technically have the same issue if a level 15 & 30 were in a party, and the level 30 gained a level.

Your only real option is to have a looping check for the party experience share.

Lua:
local function experienceShareChecker(cid)
    if isInParty(cid) then
        local party = getPartyMembers(cid)
        local canEnableExpShare = true
        local sto = getPlayerStorageValue(cid, 4875498)
      
        for i = 1, #party do
            for j = i + 1, #party do
                local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
                if levelDifference > 15 then
                    canEnableExpShare = false
                    break
                end
            end
            if not canEnableExpShare then
                break
            end
        end
      
        if party[#party] == cid then
            if canEnableExpShare then
                for i = 1, #party do
                    if sto == 2 then
                        setPlayerStorageValue(party[i], 4875498, 1)
                        doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled!")
                    else
                        setPlayerStorageValue(party[i], 4875498, 2)
                        doPlayerSendTextMessage(party[i], 22, "The Exp in Party is enabled!")
                    end
                end
            else
                doPlayerSendTextMessage(cid, 27, "The level difference between party members cannot exceed 15 levels.")
                setPlayerStorageValue(cid, 4875498, 0)
            end
        else
          
        end
    else
        doPlayerSendTextMessage(cid, 27, "You need to be a leader of a party to do that!")
    end
  
    -- add a check here to stop the looping addEvent
    -- I'm unsure in what situations you'd want to stop the talkaction, otherwise I'd add it for you.
    addEvent(experienceShareChecker, 1000, cid)
end

function onSay(cid, words, param, channel)
    experienceShareChecker(cid)
    return true       
end
I tested this code and it has an infinite loop, activating and deactivating without stopping.

Screenshot_3.png

I think the interesting thing would be to make the check be done again, calling this loop, every time a player advances in level or when a new player joins the party, just once, every time this happens, and soon after, the loop is interrupted.
 
Can you help me make this change to stop the infinite loop? @Xikini
Try this
Lua:
local function checkForPartyChanges(cid)
    if not isPlayer(cid) then
        return
    end
 
    if not isInParty(cid) then
        return
    end
 
    local sto = getPlayerStorageValue(cid, 4875498)
    local party = getPartyMembers(cid)
    local canEnableExpShare = true
 
    if sto == 1 then
        return
    end

    for i = 1, #party do
        for j = i + 1, #party do
            local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
            if levelDifference > 15 then
                canEnableExpShare = false
                break
            end
        end
        if not canEnableExpShare then
            break
        end
    end
 
    if not canEnableExpShare then
        for i = 1, #party do
            setPlayerStorageValue(party[i], 4875498, 1)
            doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled! Reason: Experience difference between party members.")
        end
        return
    end

    if canEnableExpShare then
        for i = 1, #party do
            setPlayerStorageValue(party[i], 4875498, 2)
        end
    end
 
    addEvent(checkForPartyChanges, 1000, cid)
end

function onSay(cid, words, param, channel)
   if isInParty(cid) then
       local party = getPartyMembers(cid)
       local canEnableExpShare = true
       local sto = getPlayerStorageValue(cid, 4875498)

       for i = 1, #party do
           for j = i + 1, #party do
               local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
               if levelDifference > 15 then
                   canEnableExpShare = false
                   break
               end
           end
           if not canEnableExpShare then
               break
           end
       end

       if party[#party] == cid then
           if canEnableExpShare then
               for i = 1, #party do
                   if sto == 2 then
                       setPlayerStorageValue(party[i], 4875498, 1)
                       doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled!")
                   else
                       setPlayerStorageValue(party[i], 4875498, 2)
                       doPlayerSendTextMessage(party[i], 22, "The Exp in Party is enabled!")
                       if cid == party[i] then
                           checkForPartyChanges(cid)
                           doPlayerSendTextMessage(party[i], 22, "Automatic Party Checker activated.")
                       end
                   end
               end
           else
               doPlayerSendTextMessage(cid, 27, "The level difference between party members cannot exceed 15 levels.")
               setPlayerStorageValue(cid, 4875498, 0)
           end
       else
           doPlayerSendTextMessage(cid, 27, "Only the leader of the party can do that!")
       end
   else
       doPlayerSendTextMessage(cid, 27, "You need to be a leader of a party to do that!")
   end
   return true     
end
 
Last edited:
Solution
Try this
Lua:
local function checkForPartyChanges(cid)
    if not isPlayer(cid) then
        return
    end
 
    if not isInParty(cid) then
        return
    end
 
    local sto = getPlayerStorageValue(cid, 4875498)
    local party = getPartyMembers(cid)
    local canEnableExpShare = true
 
    if sto == 1 then
        return
    end

    for i = 1, #party do
        for j = i + 1, #party do
            local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
            if levelDifference > 15 then
                canEnableExpShare = false
                break
            end
        end
        if not canEnableExpShare then
            break
        end
    end
 
    if not canEnableExpShare then
        for i = 1, #party do
            setPlayerStorageValue(party[i], 4875498, 1)
            doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled! Reason: Experience difference between party members.")
        end
        return
    end

    if canEnableExpShare then
        for i = 1, #party do
            setPlayerStorageValue(party[i], 4875498, 2)
        end
    end
 
    addEvent(checkForPartyChanges, 1000, cid)
end

function onSay(cid, words, param, channel)
   if isInParty(cid) then
       local party = getPartyMembers(cid)
       local canEnableExpShare = true
       local sto = getPlayerStorageValue(cid, 4875498)

       for i = 1, #party do
           for j = i + 1, #party do
               local levelDifference = math.abs(getPlayerLevel(party[i]) - getPlayerLevel(party[j]))
               if levelDifference > 15 then
                   canEnableExpShare = false
                   break
               end
           end
           if not canEnableExpShare then
               break
           end
       end

       if party[#party] == cid then
           if canEnableExpShare then
               for i = 1, #party do
                   if sto == 2 then
                       setPlayerStorageValue(party[i], 4875498, 1)
                       doPlayerSendTextMessage(party[i], 22, "The Exp in Party is disabled!")
                   else
                       setPlayerStorageValue(party[i], 4875498, 2)
                       doPlayerSendTextMessage(party[i], 22, "The Exp in Party is enabled!")
                       if cid == party[i] then
                           checkForPartyChanges(cid)
                           doPlayerSendTextMessage(party[i], 22, "Automatic Party Checker activated.")
                       end
                   end
               end
           else
               doPlayerSendTextMessage(cid, 27, "The level difference between party members cannot exceed 15 levels.")
               setPlayerStorageValue(cid, 4875498, 0)
           end
       else
           doPlayerSendTextMessage(cid, 27, "Only the leader of the party can do that!")
       end
   else
       doPlayerSendTextMessage(cid, 27, "You need to be a leader of a party to do that!")
   end
   return true   
end
Worked perfectly. Thank you very much! ❤️
 
Worked perfectly. Thank you very much! ❤️
I should mention some caveats with this.

If the party leader changes, there's no check to stop the loop in that scenario.
If the leader leaves the party, there's no checks for the other remaining players to stop the experience sharing.
If the server goes down / players die / logout, you need to reset the storage on login.

Probably some other stuff.

Inside the actual share experience script that is checking the storage, I would definitely re-check that people should be receiving the shared experience, and reset their storage there if they don't qualify for it anymore.

I think that'd be the ideal solution.
 
I should mention some caveats with this.

If the party leader changes, there's no check to stop the loop in that scenario.
If the leader leaves the party, there's no checks for the other remaining players to stop the experience sharing.
If the server goes down / players die / logout, you need to reset the storage on login.

Probably some other stuff.

Inside the actual share experience script that is checking the storage, I would definitely re-check that people should be receiving the shared experience, and reset their storage there if they don't qualify for it anymore.

I think that'd be the ideal solution.
I found a problem, the experience is being shared even if the players are miles away.
If one player is on the hunt, and the other is stopped in the DP, for example, the one stopped in the DP also receives experience.

How can I check if the players are a maximum of 30 square meters apart to share? If they are more than 30 sqm away from each other, the shared exp must be cancelled. @Xikini
 
Back
Top