Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 09 Mar 2014 07:47 PM |
Hey guys, I'm trying to access a table of values that belong to a player and am unsure how to proceed. I've received the suggestion to use a RemoteFunction, and it seems like it should work. However, I'm not quite sure how it should be set up. Here's generic code that should help with understanding the situation
Instances = {} -- set up a class using metatables
game.Players.ChildAdded:connect(function(player) local instance1 = newInstance() instance1.Owner = player.Name instance1.Power = 9 instance1.Position = 1
local instance2 = newInstance() instance2.Power = 11 instance2.Owner = player.Name instance2.Position = 2 end)
workspace.Encounter.Touched:connect(function(part) -- find if a player touches a part
-- use a remote function to access the correct table, determined by the instance with position = 1 RemoteFunction:InvokeServer(args) -- what should these be? end)
Right now, I think it would be something along the lines of: function RemoteFunction.OnServerInvoke:(player, position) for _,v in pairs(Instances) do if v.Owner = player.Name and v.Position==1 then return v end end end
Is this correct? Either way, is there a better way to go about it? |
|
|
| Report Abuse |
|
|
|
| 09 Mar 2014 08:14 PM |
I don't understand what your trying to do, but if you need to access something from another script just make it global
function _G.print(n) print(n) end
_G.print("Hi")
Output; Hi |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 09 Mar 2014 08:46 PM |
WarSpyKing,
I'm trying to access a system of stats that are stored in a table. Although I could create these values individually through IntValues/StringValues/etc. in a player, I thought it would be easier to store them all in one table. Because every player has the same values, I created a class the defines default values, such as HP/attack, and I want to reference an individual users stats when they start a battle with AI enemies, which occur on specific tiles/randomly while walking (i.e. a turn-based RPG). Each user can have multiple sets of stats, which makes it harder to pinpoint the values I need. Due to this, I was thinking of setting a position/priority value to the stat set to know which one should load. |
|
|
| Report Abuse |
|
|
|
| 09 Mar 2014 08:48 PM |
1 use player added not child added 2 use instance.new("IntValue") --value type not newinstance()
|
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 09 Mar 2014 08:55 PM |
DryBones,
I've cut a significant portion of code in between the use of newinstance(). I'm not trying to hold an integer value, but rather an entire set of values. Here's some more of what's going on:
do local InstanceDefaults = { Name = ""; Class = ""; Level = 1; Experience = 0; Type = nil; Stats = { HP = 25; Melee = 10; Defense = 15; Magic = 10; Resistance = 15; Speed= 20; }; Moves = {}; Owner; -- player } local InstanceMetatable = { __index = function(t,i) return InstanceHolder[t][i] or InstanceClassDefaults[i] end; __newindex = function(t,i,v) InstanceHolder[t][i] = v; end; }
function newInstance() local instance = {} InstanceHolder[instance] = {}; return setmetatable(instance, InstanceMetatable) end end
Are you saying I have the ability to store tables in an IntValue? |
|
|
| Report Abuse |
|
|
|
| 09 Mar 2014 09:01 PM |
| An easy way would be to encode the table as a JSON put it in a string value then have the server decode it back into a lua table. |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 09 Mar 2014 09:11 PM |
Notsopenguin,
So, it would look something like this, right?
game.Players.ChildAdded:connect(function(player) instance1 = newInstance()
-- modify instance in order to fill in any changes form default class here
JSON = Utils.EncodeJSON(instance1)
access1 = Instance.new("StringValue", player) access1.Name = "Stats1" access1.Value = JSON
end)
function encounterStart(player) stats = Utils.DecodeJSON(player.Stats1.Value) -- stuff end
How does this interact with non-set values with the metatables (e.g. defining a different magic stat before encoding but not declaring a melee stat, using the class' default value of 10). Does the encryption pick them up due to the __index function and/or will it continue to behave normally and realize it is an instance of the class/metatable after decoding?
|
|
|
| Report Abuse |
|
|
|
| 10 Mar 2014 07:11 AM |
Instead of storing everyones values in the one table, make a different table per user? StatsForP1 = {} StatsForP2 = {} StatsForP3 = {} StatsForP4 = {} StatsForP5 = {} StatsForP6 = {} StatsForP7 = {} StatsForP8 = {} StatsForP9 = {} StatsForP10 = {} game.Players.PlayerAdded:connect(function(p) ps = game.Players:GetPlayers() pl = #ps + 1 table.insert(StatsForP[pl], STAT) --Continue PLAYER = Instance.new("NumberValue") PLAYER.Parent = p PLAYER.Value = pl PLAYER.Name = "PLAYER" end)
I don't know if it helps any, im also not sure about how I inserted the stats, but it may help. |
|
|
| Report Abuse |
|
|