More actions
How to use avatar vars |
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 | 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 | ||
<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. | ||
<syntaxhighlight lang="lua"> | |||
for uuid, vars in pairs(world.avatarVars()) do | |||
print(uuid) | |||
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: | ||
<syntaxhighlight lang="lua"> | |||
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 | |||
</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