Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I get data from server Module Script to local PlayerGui?

Asked by 6 years ago

I'm trying to get my player's session data stored in a Module Script inside ServerStorage and display the value in a TextBox in the Player's Gui locally. However, my remote function isn't running through the entire script and it stopping its process in the middle.

The Local Script inside the TextBox. I'm not printing any value at all and the 3rd line of code is never reached. No errors/exceptions are thrown.

print("Starting")
print(game.ReplicatedStorage.GetPlayerData:InvokeServer("swordSkill"))
print("Finished")

The Script inside the Remote Function

script.Parent.OnServerInvoke:Connect(function (player,statName)
    local dataTable = game.ServerStorage.PlayerStatManager:GetPlayerSessionData()
    return dataTable[player][statName]
end)

My ModuleScript inside ServerStorage (Based of the model on the scripting wiki)

--What's returned to the scripts calling on this ModuleScript
local PlayerStatManager = {}

--Create DataStore variables
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

--Autosave interval
local AUTOSAVE_INTERVAL = 120

--Table that holds all the players' data for this session
local sessionData = {}

--Function that can be called from other scripts
--Function is placed within PlayerStatManager so external scripts can reach it
function PlayerStatManager:ChangeStat(player, statName, newValue)
    sessionData[player][statName] = newValue
end

function PlayerStatManager:GetPlayerSessionData(player)
    return sessionData[player]
end

--Function retrieves player data from the DataStoreService
local function getPlayerData(player)
    return playerData:GetAsync(player.UserId)
end

--Function saves player data to the DataStoreService
local function savePlayerData(player)
    playerData:SetAsync(player.UserId, sessionData[player])
end

--Generates a new table of player skills
local function newSkillsTable()
    return {swordSkill = 1, shieldSkill = 1, speechSkill = 1}
end

--Function adds new player to the session table
local function setupPlayerData(player)
    local data = getPlayerData(player)
    if not data then
        --No data found
        sessionData[player] = newSkillsTable()
        savePlayerData(player)
    else
        sessionData[player] = data  
    end
end

--Autosave function
local function autosave()
    while wait(AUTOSAVE_INTERVAL) do
        for player, data in pairs(sessionData) do
            savePlayerData(player)
        end
    end
end

--Binds setupPlayerData to PlayerAdded
game.Players.PlayerAdded:Connect(setupPlayerData)

--Save a player's data when they leave
game.Players.PlayerRemoving:Connect(function(player)
    savePlayerData(player)
    sessionData[player] = nil
end)

--Run autosave function in the background
spawn(autosave)

--Returns PlayerStatManager so it can be accessed externally
return PlayerStatManager

Answer this question