generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Please help with this

Previous Thread :: Next Thread 
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
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
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
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
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
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
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
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
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
28 Dec 2016 09:19 PM
Oh okay thanks pal!
Report Abuse
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
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
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
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
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
28 Dec 2016 09:29 PM
Are there any errors?


Report Abuse
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
28 Dec 2016 09:30 PM
No
Report Abuse
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
28 Dec 2016 09:44 PM
b
Report Abuse
skppiy4000 is online. skppiy4000
Joined: 09 Oct 2008
Total Posts: 1945
28 Dec 2016 11:47 PM
b2
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image