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

Could this module script be used on a local script without errors?

Asked by 4 years ago

This question is, in a way, a continuation of an earlier question I asked here. Earlier, I found out that my module script I wanted to use in a local script had yielded due to the fact that some code may not be suitable to run in a local script. The thing is, I'm not sure what can't be ran. Below is the code I wanted to use in my local script to save some data.

-- Set up table to return to any script that requires this module script
local SaveFunctions = {}

local playerData = game:GetService("ReplicatedStorage"):WaitForChild("DataStoreRequiringFunction"):Invoke()

-- Table to hold player information for the current session
local sessionData = {}

-- 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")
    sessionData[playerUserId][statName] = value
end

-- Function that other scripts can call to get a player's stats
function PlayerStatManager.GetStat(player, statName)
    local playerUserId = "Player_" .. player.UserId
    while wait() do
        if sessionData[playerUserId] then
            if sessionData[playerUserId][statName] then
                break
            end
        end
    end
    local value = sessionData[playerUserId][statName]
    return value
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
            -- Data exists for this player
            sessionData[playerUserId] = data
        else
            -- Data store is working, but no current data for this player
            sessionData[playerUserId] = {
            Level = 1,
            Exp = 0
            }
        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(30) do
        for playerUserId, data in pairs(sessionData) do
            savePlayerData(playerUserId)
        end
    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 SaveFunctions

Which code should I edit? (Btw, I renamed my module script, which is why I'm using different names from the last question I asked, which I had linked to in this post)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

1) You can't use DataStore on the client at all. Why are you requiring this DataStore manager from the client?

2) What is PlayerStatManager. I can seem to find it anywhere.

3) Can you explain to my what DataStoreRequiringFunction is? It seems to me that it's heavily based on DataStore which again you can't use on the client.

IIRC it errored when you tried to use DataStore on the client or something.

0
DataStoreRequiringFunction is a function I put in ReplicatedStorage to connect the module with a script in ServerScriptService to obtain the data store that stores the player data. I thought it might help to avoid any errors when used in local scripts. The main problem for me when I use this module though, is that I use it on both normal scripts and local scripts, so I'm not sure what to do :/ ServerLover38 21 — 4y
0
Do I have to connect the local scripts using functions to normal scripts which will require the module instead? Sorry, I'm struggling to use these data stores, I'm really new to them. ServerLover38 21 — 4y
0
In the ServerScriptService? I think I see where part of the problem is. it can't be called because Roblox never tells the players what's in SSS GGRBXLuaGG 417 — 4y
0
It's totally fine. I have not seen anyone who didn't struggle with DataStore without any past experience with data saving on servers. can you tell me the behaviour you are trying to achieve by calling the module from the client? GGRBXLuaGG 417 — 4y
View all comments (6 more)
0
I'm using this module to help run code on the client which can obtain or manipulate the player's data whenever necessary. ServerLover38 21 — 4y
0
You seem to have a big issue there. You see, you want to give the player access to change their data. This is very very dangerous because of exploiters, while you can't use DS on the client your idea is trusting the client too much. The server should be the only one to determine everything on the DS side. GGRBXLuaGG 417 — 4y
0
What I would do instead is 1) make a RE. 2) and when it's fired by a client it would get the data from DS from that client and fire the RE for that client. 3) connect a function on the client for that RE so that when it's fired (when the server responds with the data) it would update it for the client GGRBXLuaGG 417 — 4y
0
I can get more in depth if you want. Just PM and I will help :D GGRBXLuaGG 417 — 4y
0
I see, okay, I'll try that, thanks for helping out and bearing with me! ServerLover38 21 — 4y
0
So glad I was able to help you. I hope you would be able to do it now :D GGRBXLuaGG 417 — 4y
Ad

Answer this question