I want a function to run and load the player data when a player joins the game. I have put in two print functions and the first one runs that says that it is calling the script but the second one doesn’t. Below is the script and there is also a picture of the output. Any help would be really appreciated.
local playerService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")
local PlayerHandler = {}
local sessionData = {}
local function setupPlayerData(player)
print("trying to run...")
local playerUserId = player.UserId
local data
local success = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success then
if data then
sessionData[playerUserId] = data
print(sessionData[playerUserId].Level, sessionData[playerUserId].Xp)
player.Character.AddXpServer.Level.Value = sessionData[playerUserId].Level
player.Character.AddXpServer.Xp.Value = sessionData[playerUserId].Xp
else
sessionData[playerUserId] = {Level = 0, Xp = 0, Inventory = {}}
end
end
end
local function saveData(player)
local playerUserId = player.UserId
local success = pcall(function()
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end)
end
function PlayerHandler.ChangeValue(player, name, value)
local playerUserId = player.UserId
if sessionData[playerUserId] then
sessionData[playerUserId][name] = value
end
end
playerService.PlayerAdded:Connect(setupPlayerData, print("calling function"))
playerService.PlayerRemoving:Connect(saveData)
return PlayerHandler
Except for the
function PlayerHandler.ChangeValue(player, name, value)
local playerUserId = player.UserId
if sessionData[playerUserId] then
sessionData[playerUserId][name] = value
end
end
And also except for the returning and declaring the module, move the other script to a normal script
What is the “except for the” referring to and also, what other script, do you mean the script that is changing the data values? If so then that script is inside of StarterCharacterScripts and is a server script.
Put these in your module script:
local PlayerHandler = {}
function PlayerHandler.ChangeValue(player, name, value)
local playerUserId = player.UserId
if sessionData[playerUserId] then
sessionData[playerUserId][name] = value
end
end
return PlayerHandler
Put these in a script in ServerScriptService:
local playerService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")
local sessionData = {}
local function setupPlayerData(player)
print("trying to run...")
local playerUserId = player.UserId
local data
local success = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success then
if data then
sessionData[playerUserId] = data
print(sessionData[playerUserId].Level, sessionData[playerUserId].Xp)
player.Character.AddXpServer.Level.Value = sessionData[playerUserId].Level
player.Character.AddXpServer.Xp.Value = sessionData[playerUserId].Xp
else
sessionData[playerUserId] = {Level = 0, Xp = 0, Inventory = {}}
end
end
end
local function saveData(player)
local playerUserId = player.UserId
local success = pcall(function()
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end)
end
playerService.PlayerAdded:Connect(setupPlayerData, print("calling function"))
playerService.PlayerRemoving:Connect(saveData)
Okay - i will try that. Thanks for spending so much time working on an answer btw, im rlly grateful.
Thank you for the reply! It will be appreciated if you would set it as a solution as it also helps others to find it faster. Thank you!
So inside of the server script, I’m saving the data using the second script that you posted (that goes inside of ServerScriptSerice) and then using the module script to change the values. There seems to be a minor problem and it is that they are not linked together (I’m trying to change that now ). Therefore I cannot get the session data for the module script from the server one. I will keep trying and as soon as I get it working, I will post the tinkered scripts and mark the appropriate solution. Also, just a side note, thanks for helping with this, I’m really struggling with using datastores so having your help is amazing
1 Like
I know just how to link them together. Can you tell me where did you put the module script? After that I’ll be able to help you link them together
I put the module script inside of server storage
Ok. I will type the code to you. Give me a minute
Wait what is the name of your module script? I need it to find the path to require it from the server script
its called “PlayerDataHandler”.
Excuse me, but I want to need to know what is the Name argument in your module script that you need? Like where do you get that argument from? In the function ChangeValue the line that asksfor three arguments (player, name, value). I need to know where do you get the name and value from.
Okay so I gotta me honest with you. I dont have much experience with working with datastore so i have used this model/video in the past. Just copied it and modified it to work with the system that I already had in place so that it would just save the values. The video is here and it has worked in the past: Advanced Datastore Tutorial - Roblox Studio - YouTube
I thought that the three values were defined using the player’s id (value = players user id or something like that) but looking back through the video, it cant see anything like that. I feel like I might have to just wipe what I have and start afresh using a different system that I build.
I will try to help you with it
This is the solution:
Put these in the module script:
local playerService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")
local PlayerHandler = {}
local sessionData = {}
local function setupPlayerData(player)
local playerUserId = player.UserId
local data
local success = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success then
if data then
sessionData[playerUserId] = data
print(sessionData[playerUserId].Money)
else
sessionData[playerUserId] = {Money = 0, Inventory = {}}
end
end
end
local function saveData(player)
local playerUserId = player.UserId
local success = pcall(function()
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end)
end
function PlayerHandler.ChangeValue(player, name, value)
local playerUserId = player.UserId
if sessionData[playerUserId] then
sessionData[playerUserId][name] = value
end
end
playerService.PlayerAdded:Connect(setupPlayerData)
playerService.PlayerRemoving:Connect(saveData)
return PlayerHandler
Put these in the server script:
local playerHandler = require(game.ServerStorage:WaitForChild("PlayerHandler"))
game.Players.PlayerAdded:Connect(function(player)
wait(1)
playerHandler.ChangeValue(player, "Money", 50)
end)
This is now the correct one. It would be appreciated to be set as solution