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

I was making a script to store stats but GetChildren in line 20 is invalid, how do I fix this?

Asked by 3 years ago
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("Test 1")

local baseStats =
{
    Health = 10;
    Stamina = 100;
    Speed = 16;
    Strength = 5;
    Agility = 0;
    Gold = 100;
    Level = 1;
    EXP = 0;
    }

function saveData(Player,PlrStats)
    local dataToSave = {}

    warn("Saving Data...")
    for i, data in pairs(PlrStats:GetChildren()) do
        dataToSave[i] = data.Value
    end

    GameData:SetAsync(Player.UserId,dataToSave)
    warn("Data Saved")
end

game.Players.PlayerAdded:Connect(function(Player)
    local PlrStats = Instance.new("Folder",Player)
    PlrStats.Name = "PlrStats"

    local Health = Instance.new("NumberValue",PlrStats)
    Health.Name = "Health"
    Health.Value = baseStats.Health

    local Stamina = Instance.new("NumberValue",PlrStats)
    Stamina.Name = "Stamina"
    Stamina.Value = baseStats.Stamina

    local Speed = Instance.new("NumberValue",PlrStats)
    Speed.Name = "Speed"
    Speed.Value = baseStats.Speed

    local Strength = Instance.new("NumberValue",PlrStats)
    Strength.Name = "Strenght"
    Strength.Value = baseStats.Strength

    local Agility = Instance.new("NumberValue",PlrStats)
    Agility.Name = "Agility"
    Agility.Value = baseStats.Agility

    local Gold = Instance.new("NumberValue",PlrStats)
    Gold.Name = "Gold"
    Gold.Value = baseStats.Gold

    local Level = Instance.new("NumberValue",PlrStats)
    Level.Name = "Level"
    Level.Value = baseStats.Level

    local EXP = Instance.new("NumberValue",PlrStats)
    EXP.Name = "EXP"
    EXP.Value = baseStats.EXP

    local plrSaves
    pcall(function()
        plrSaves = GameData:GetAsync(Player.UserId)
    end)

    if plrSaves then
        --/If Player has Data
        warn("Player has Data")
        for i, data in pairs(PlrStats:GetChildren()) do
            data.Value = plrSaves[i]
            print(data.Name,":",data.Value)
        end 

    else
        --/If Player doesn't have Data
        warn("Player has no Data")
        for i, data in pairs(PlrStats:GetChildren()) do
            print(data.Name,":",data.Value)
        end 

    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local PlrStats = Player:WaitForChild("PlrStats")
    saveData()(Player,PlrStats)
end)
0
look line 90 enzotinman1 23 — 3y
0
Thanks that fixed the issue AlphaFlame5 -1 — 3y

Answer this question