|
| 28 Dec 2016 09:09 PM |
-- Setup table that we will return to scripts that require the ModuleScript. local PlayerStatManager = {} -- Table to hold all of the player information for the current session. local sessionData = {} -- Function the other scripts in our game can call to change a player's stats. This -- function is stored in the returned table so external scripts can use it. function PlayerStatManager:ChangeStat(player, statName, changeValue) sessionData[player][statName] = sessionData[player][statName] + changeValue end
-- Function to add player to the sessionData table. local function setupPlayerData(player) sessionData[player] = {Money = 0, Experience = 0} end -- Bind setupPlayerData to PlayerAdded to call it when player joins. game.Players.PlayerAdded:connect(setupPlayerData) -- Return the PlayerStatManager table to external scripts can access it. return PlayerStatManager
Im making a basic currency system that saves but theres a problem, Im not sure how to make it so localscripts or scripts can access it so you can read its value (Like a gui that says how much money you have) |
|
|
| Report Abuse |
|
|
|
| 28 Dec 2016 09:16 PM |
Other scripts should be able to access it by requiring the module. For LocalScripts, just create a stat inside of their player that will mirror the value inside this script:
local Stats = { Money = 0; Experience = 0; }
function PlayerStatManager:ChangeStat(player, statName, changeValue) sessionData[player][statName] = sessionData[player][statName] + changeValue local stat = player:FindFirstChild("Stats") and player.Stats:FindFirstChild(statName) if stat then stat.Value = changeValue end end
local function setupPlayerData(player) sessionData[player] = CloneTable(Stats) local stats = Instance.new("Foler") stats.Name = "Stats" for i,v in pairs(Stats) do local statObj = Instance.new("IntValue") statObj.Name = i statObj.Value = v statObj.Parent = stats end stats.Parent = player end
|
|
|
| Report Abuse |
|
|
|
| 28 Dec 2016 09:18 PM |
Then to access it in a localscript:
local playerStats = game.Players.LocalPlayer:WaitForChild("Stats") local money = playerStats:WaitForChild("Money")
print(money.Value)
|
|
|
| Report Abuse |
|
|
|
| 28 Dec 2016 09:18 PM |
| A little bit more explanation what I'm supposed to do here. Sorry if I sound like a idiot, this stuff confuses me a tad. |
|
|
| Report Abuse |
|
|
| |
|
|
| 28 Dec 2016 09:22 PM |
Wait, I forgot something. Add this under the Stats:
function CloneTable(table) local clone = {} for i,v in pairs(table) do clone[i] = v end return clone end
|
|
|
| Report Abuse |
|
|
|
| 28 Dec 2016 09:27 PM |
-- Setup table that we will return to scripts that require the ModuleScript. local PlayerStatManager = {}
local Stats = { Money = 0; Experience = 0; }
function CloneTable(table) local clone = {} for i,v in pairs(table) do clone[i] = v end return clone end
-- Table to hold all of the player information for the current session. local sessionData = {} -- Function the other scripts in our game can call to change a player's stats. This -- function is stored in the returned table so external scripts can use it. function PlayerStatManager:ChangeStat(player, statName, changeValue) sessionData[player][statName] = sessionData[player][statName] + changeValue local stat = player:FindFirstChild("Stats") and player.Stats:FindFirstChild(statName) if stat then stat.Value = changeValue end end
-- Function to add player to the sessionData table. local function setupPlayerData(player) sessionData[player] = CloneTable(Stats) local stats = Instance.new("Folder") stats.Name = "Stats" for i,v in pairs(Stats) do local statObj = Instance.new("IntValue") statObj.Name = i statObj.Value = v statObj.Parent = stats end stats.Parent = player end -- Bind setupPlayerData to PlayerAdded to call it when player joins. game.Players.PlayerAdded:connect(setupPlayerData) -- Return the PlayerStatManager table to external scripts can access it. return PlayerStatManager
No folder ever is put into player even in testing local |
|
|
| Report Abuse |
|
|
| |
|
| |
|
| |
|
| |
|