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

How would access a value inside a player?

Asked by 4 years ago

I am trying to make a script that saves multiple values I can save the value if I set it directly inside the datatosave table but for some reason when I try to save the value from the player It says the wring value. I used a print statement right before the save to detect that its not the save function that's the problem but just accessing the correct data. What's your thoughts?

--Get The Service's
local replicatedStorage = game:GetService("ReplicatedStorage")
local dsService = game:GetService("DataStoreService")
local mainStore = dsService:GetDataStore("MainDataStore")

local DataToSave = {}
--create player data
game:GetService("Players").PlayerAdded:Connect(function(plr)

    --create folders

    local leader = Instance.new("Folder",plr)
    leader.Name = "leaderstats"

    local achievementsFolder = Instance.new("Folder",leader)
    achievementsFolder.Name = "Achievements"

    local gameplayStats = Instance.new("Folder",leader)
    gameplayStats.Name = "GameplayStats"

    local playerStats = Instance.new("Folder",leader)
    playerStats.Name = "PlayerStats"


    --create achievements Values

    CompletedTutorial = Instance.new("BoolValue",achievementsFolder)
    CompletedTutorial.Name = "CompletedTutorialAchievement"
    CompletedTutorial.Value = false


    --create gameplay values

    CompletedMissions = Instance.new("IntValue",gameplayStats)
    CompletedMissions.Name = "CompletedMissions"
--  CompletedMissions.Value = 12

    FailedMissions = Instance.new("IntValue",gameplayStats)
    FailedMissions.Name = "FailedMissions"
    FailedMissions.Value = 0

    PreviousMission = Instance.new("StringValue",gameplayStats)
    PreviousMission.Name = "PreviousMission"
    PreviousMission.Value = "No Missions Attempted"

    TimesDied = Instance.new("IntValue",gameplayStats)
    TimesDied.Name = "TimesDied"
    TimesDied.Value = 0

    TrapsAvoided = Instance.new("IntValue",gameplayStats)
    TrapsAvoided.Name = "TrapsAvoided"
    TrapsAvoided.Value = 0

    TrapsFailed = Instance.new("IntValue",gameplayStats)
    TrapsFailed.Name = "TrapsFailed"
    TrapsFailed.Value = 0


    --create player values

    Coins = Instance.new("IntValue",playerStats)
    Coins.Name = "Coins"
    Coins.Value = 0


    local currentDay = os.date("*t").day
    local currentMonth = os.date("*t").month
    local currentYear = os.date("*t").year

    local dateJoined = (currentDay.."/"..currentMonth.."/"..currentYear)

    DateFirstJoined = Instance.new("StringValue",playerStats)
    DateFirstJoined.Name = "DateFirstJoined"
    DateFirstJoined.Value = dateJoined

    Rank = Instance.new("StringValue",playerStats)
    Rank.Name = "Rank"
    Rank.Value = "Junior Assistant"

    totalTime = Instance.new("IntValue",playerStats)
    totalTime.Name = "TotalTimePlayed"
    totalTime.Value = 0

    timePlayed = Instance.new("IntValue",playerStats)
    timePlayed.Name = "TimePlayed"
    timePlayed.Value = 0

    Experience = Instance.new("IntValue",playerStats)
    Experience.Name = "Experience"
    Experience.Value = 0

    wait(1)

    --load the data
    local success, err = pcall(function()
        local data = mainStore:GetAsync(plr.UserId) 
            --gameplay values
            CompletedMissions.Value = data[1] or 5
            --[[FailedMissions.Value = data[2]
            PreviousMission.Value = data[3]
            TimesDied.Value = data[4]
            TrapsAvoided.Value = data[5]
            TrapsFailed.Value = data[6]

            --player stats value
            Coins.Value = data[7]
            DateFirstJoined.Value = data[8]
            Rank.Value = data[9]
            totalTime.Value = data[10]
            Experience.Value = data[11]

            print(CompletedMissions.Value, FailedMissions.Value, PreviousMission.Value, TimesDied.Value, TrapsAvoided.Value, TrapsFailed.Value, Coins.Value, DateFirstJoined.Value, Rank.Value, totalTime.Value, Experience.Value)
            --]]
            print(CompletedMissions.Value)
    end)
    if err then
        print("error loading data")
    end
end)

--Increment Time Values
incrementor = coroutine.create(function()
    while wait(1) do
        timePlayed.Value = timePlayed.Value + 1
        totalTime.Value = totalTime.Value + 1
    end
end)

coroutine.resume(incrementor)

CompletedMissions.Changed:connect(function()
    DataToSave[1] = CompletedMissions.Value
    print("changed value to"..DataToSave[1])
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)

    --[[
    local DataToSave = {CompletedMissions.Value, FailedMissions.Value, PreviousMission.Value, TimesDied.Value, TrapsAvoided.Value, TrapsFailed.Value, 
    Coins.Value, DateFirstJoined.Value, Rank.Value, totalTime.Value, Experience.Value}
    --]]

    print(player.leaderstats.GameplayStats.CompletedMissions.Value)

    local success, err = pcall(function()
        mainStore:SetAsync(player.UserId,DataToSave)
    end)
    if err then
        print("error saving")
    end
end)


Answer this question