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

What's the current issue with the Data Module Script below?

Asked by 7 years ago
local DefaultData = {["MultiplierLevel"] = 1, ["NetWorth"] = 0, ["Cash"] = 25, ["RobuxLevel"] = 0, ["TixLevel"] = 0, ["MoneybagLevel"] = 0, ["AutoClickerLevel"] = 0, ["HealthLevel"] = 0, ["DamageLevel"] = 0,["Colour"] = BrickColor.new("Really red"), ["Material"] = "Brick"}

print("Updated")
local PlayerStats = {}

local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("Data")

local Interval = 10

local CurrentData = {}
local LoadedPlayers = {}

function PlayerStats:IsPlayerLoaded(Player)
    local bool = LoadedPlayers[Player.Name] or false
    return bool
end

function PlayerStats.ChangeStat(Player, Stat, Value)
    if CurrentData[Player.Name] and CurrentData[Player.Name][Stat] then
        CurrentData[Player.Name][Stat] = Value
    else
        table.insert(CurrentData[Player.Name], Stat, DefaultData[Stat])
    end
    print(Value)
end

function PlayerStats.AddStat(Player, Stat, Value)
    if CurrentData[Player.Name] and CurrentData[Player.Name][Stat] then
        CurrentData[Player.Name][Stat] = CurrentData[Player.Name][Stat] + Value
    else
        table.insert(CurrentData[Player.Name], Stat, DefaultData[Stat])
    end
end
function PlayerStats.SubStat(Player, Stat, Value)
    if CurrentData[Player.Name] and CurrentData[Player.Name][Stat] then
        CurrentData[Player.Name][Stat] = CurrentData[Player.Name][Stat] - Value
    else
        table.insert(CurrentData[Player.Name], Stat, DefaultData[Stat])
    end
end

function PlayerStats.GetPlayerData(Player)
    return PlayerData:GetAsync(Player.UserId)
end

function PlayerStats.SetPlayerData(Player)
    if PlayerStats:IsPlayerLoaded(Player) then
        PlayerData:SetAsync(Player.UserId, CurrentData[Player.Name])
    end
end

function PlayerStats.ReturnCurrentData(Player, Stat)
    local dataTable = CurrentData[Player.Name]
    local data = DefaultData[Stat] or 0
    if dataTable ~= nil then data = dataTable[Stat] or DefaultData[Stat] end
    return data
end

local function NewPlayerData(Player)
    print("Player Joined")
    local Data = PlayerStats.GetPlayerData(Player)
    if not Data then
        CurrentData[Player.Name] = DefaultData
        PlayerStats.SetPlayerData(Player)
    else
        CurrentData[Player.Name] = Data
    end
    --CurrentData[Player.Name]["MultiplierLevel"], CurrentData[Player.Name]["NetWorth"], CurrentData[Player.Name]["Cash"], CurrentData[Player.Name]["RobuxLevel"], CurrentData[Player.Name]["TixLevel"], CurrentData[Player.Name]["MoneybagLevel"], CurrentData[Player.Name]["AutoClickerLevel"], CurrentData[Player.Name]["HealthLevel"], CurrentData[Player.Name]["DamageLevel"]
    LoadedPlayers[Player.Name] = true
    print(Player.Name, CurrentData[Player.Name]["MultiplierLevel"], CurrentData[Player.Name]["NetWorth"], CurrentData[Player.Name]["Cash"], CurrentData[Player.Name]["RobuxLevel"], CurrentData[Player.Name]["TixLevel"], CurrentData[Player.Name]["MoneybagLevel"], CurrentData[Player.Name]["AutoClickerLevel"], CurrentData[Player.Name]["HealthLevel"], CurrentData[Player.Name]["DamageLevel"])
    --print(Player.Name, CurrentData[Player.Name]["MultiplierLevel"], )
end

game.Players.PlayerAdded:Connect(function(Player)
    NewPlayerData(Player)
end)

function Autosave()
    --_G.NotifyPlayer("Server Notification", "Data Auto Saved", 5) --TESTING
    while wait(Interval) do
        for Player, Data in pairs(CurrentData) do
            PlayerStats.SetPlayerData(Player)
        end
    end
end

game.Players.PlayerRemoving:Connect(function(Player)
    PlayerStats.SetPlayerData(Player)
    CurrentData[Player.Name] = nil
end)

spawn(Autosave)

return PlayerStats

For some unknown reason, I seem to be getting no errors in the developer's log ingame, but in studio, I seem to be getting an API rejection issue. I have enabled access to the API for the script in configure game, but it doesn't seem to help. In addition to such, in both studio and ingame, it never actually saves the values. Instead it just comes back out with the old default values, which poses an issue.

0
Sorry for making it so much code, but I'm unsure what's really irrelevant and relevant in a situation like this. animorphs30 300 — 7y

1 answer

Log in to vote
0
Answered by
Jellyfosh 125
7 years ago
Edited 7 years ago

In your table, your values do not need to be defined with brackets and quotes, I.E. ["Material"] = "Brick" could simply be Material = "Brick". I'm pretty sure your problem comes from:

["Colour"] = BrickColor.new("Really red")

In my experience, tables don't like doing "new" things. You'll have to do Color = "Really red" and then whenever you want to find it use:

local color = BrickColor.new(DefaultData["Color"])

Try something like that and see if you get any errors.

Ad

Answer this question