Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Multiplayer Avatar Interaction: Difference between revisions

From FiguraMC
Manuel (talk | contribs)
How to use avatar vars
 
PenguinEncounter (talk | contribs)
a bit of style cleanup
Tag: 2017 source edit
 
Line 1: Line 1:
Avatars can share values with one another to allow for interactions between them. An example of this would be the various petting scripts you can find in the community.
Avatars can share values with one another to allow for interactions between them. An example of this would be the various petting scripts you can find in the community.


The basic princible works like this:
The basic principle works like this:
One avatar can store a value.
One avatar can store a value.
Another avatar can get this stored value and do something based on it.
Another avatar can get this stored value and do something based on it.
Line 8: Line 8:


For example
For example
avatar:store("amount", 1)
<syntaxhighlight lang="lua">
avatar:store("amount", 1)
</syntaxhighlight>


Note: It is recommended to never store any UserData objects as this could lead to security issues! Best to only store primitives like numbers, strings, or regular tables!
Note: It is recommended to never store any UserData objects as this could lead to security issues! Best to only store primitives like numbers, strings, or regular tables!
Line 14: Line 16:
Someone else can then read this value in their script by checking the avatarVars for each player.
Someone else can then read this value in their script by checking the avatarVars for each player.


for uuid, vars in pairs(world.avatarVars()) do
<syntaxhighlight lang="lua">
    print(uuid)
for uuid, vars in pairs(world.avatarVars()) do
    printTable(vars)
    print(uuid)
end
    printTable(vars)
end
</syntaxhighlight>


So for our example you could read the "amount" which we set to 1 and show the player name as well:
So for our example you could read the "amount" which we set to 1 and show the player name as well:


for uuid, vars in pairs(world.avatarVars()) do
<syntaxhighlight lang="lua">
    if vars["amount"] then
for uuid, vars in pairs(world.avatarVars()) do
        local playerName = uuid
    if vars["amount"] then
        for name, plr in pairs(world.getPlayers()) do
        local playerName = uuid
            if plr:getUUID() == uuid then
        for name, plr in pairs(world.getPlayers()) do
                playerName = name
            if plr:getUUID() == uuid then
            end
                playerName = name
        end
            end
        print(playerName, "has amount of", vars[key])
        end
    end
        print(playerName, "has amount of", vars[key])
end
    end
end
</syntaxhighlight>

Latest revision as of 05:12, 14 April 2025

Avatars can share values with one another to allow for interactions between them. An example of this would be the various petting scripts you can find in the community.

The basic principle works like this: One avatar can store a value. Another avatar can get this stored value and do something based on it.

To store a value that other avatars can read you use avatar:store(key, value)

For example

avatar:store("amount", 1)

Note: It is recommended to never store any UserData objects as this could lead to security issues! Best to only store primitives like numbers, strings, or regular tables!

Someone else can then read this value in their script by checking the avatarVars for each player.

for uuid, vars in pairs(world.avatarVars()) do
    print(uuid)
    printTable(vars)
end

So for our example you could read the "amount" which we set to 1 and show the player name as well:

for uuid, vars in pairs(world.avatarVars()) do
    if vars["amount"] then
        local playerName = uuid
        for name, plr in pairs(world.getPlayers()) do
            if plr:getUUID() == uuid then
                playerName = name
            end
        end
        print(playerName, "has amount of", vars[key])
    end
end