I have been folowing the Roblox developpers data storing tutorial "Saving Player Data" from where I got the module script code.
http://developer.roblox.com/en-us/articles/Saving-Player-Data
-- Set up table to return to any script that requires this module script
local PlayerStatManager = {}
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
-- Table to hold player information for the current session
local sessionData = {}
local AUTOSAVE_INTERVAL = 60
-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
local playerUserId = "Player_" .. player.UserId
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
print(sessionData[playerUserId][statName])
else
sessionData[playerUserId][statName] = value
end
end
-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
local playerUserId = "Player_" .. player.UserId
local success, data = pcall(function()
return playerData:GetAsync(playerUserId)
end)
if success then
if data then
print("Data exists for this player")
sessionData[playerUserId] = data
else
print("Data store is working, but no current data for this player")
sessionData[playerUserId] = {Money=18, Exp=17}
end
else
warn("Cannot access data store for player!")
end
end
-- Function to save player's data
local function savePlayerData(playerUserId)
if sessionData[playerUserId] then
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end)
if not success then wait(1) end
until
tries == 3 or success
if not success then
warn("Cannot save data for player!")
end
end
end
-- Function to save player data on exit
local function saveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId)
end
-- Function to periodically save player data
local function autoSave()
while wait(AUTOSAVE_INTERVAL) do
for playerUserId, data in pairs(sessionData) do
savePlayerData(playerUserId)
end --sauvegarde les donnés
end
end
-- Start running "autoSave()" function in the background
spawn(autoSave)
-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)
-- Connect "saveOnExit()" function to "PlayerRemoving" event
game.Players.PlayerRemoving:Connect(saveOnExit)
-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager
What I am trying to do is to use this function in another script to change Money's variable value :
function PlayerStatManager:ChangeStat(player, statName, value)
To do so I added return PlayerStatManager
to the module script.
Here is the test script i did to use that function:
local PlayerStatManager = require(game.ServerScriptService.DataModule)
local Money = require(game.ServerScriptService.DataModule)
local value = 10 -- can't insert a number as an argument, "expected identifier when parsing varible name, got '10'" error
local i = 0
repeat
wait(1)
i = i + 1
function PlayerStatManager:ChangeStat(player, Money, value)
end
print(i)
until
i==4
Here is the error i get :
"ServerScriptService.DataModule:17: attempt to index nil with 'UserId' "
This mean for me that player, as an argument is nil, wich I don't understand because it's used like that in the module script.
I also can't isert directly a number as an argument of the function, i have the folowing error:
" expected identifier when parsing varible name, got '10' "
To sum up :
Why 'player' is nil as an argument despite it is used like that in the module script?
Why I can't use a number as an argument?
What is the correct code to use the function in another script?
Here is the full output if it can be useful :
17:15:09.409 - ServerScriptService.DataModule:17: attempt to index nil with 'UserId' 17:15:09.409 - Stack Begin 17:15:09.409 - Script 'ServerScriptService.DataModule', Line 17 - function ChangeStat 17:15:09.410 - Script 'ServerScriptService.Leaderboards', Line 10 17:15:09.410 - Stack End Data exists for this player 17:15:10.766 - ERROR running in studio Data loaded 1 2 3 4 17:15:11.546 - MouseButton1Click is not a valid member of ImageLabel 17:15:11.547 - Stack Begin 17:15:11.547 - Script 'Players.Aut0_doubleAK47.PlayerGui.Backpack Gui.Frame.inventaire', Line 4 17:15:11.547 - Stack End 17:15:21.998 - Disconnect from 127.0.0.1|58624
