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

Datastore not saving and random values are the same everytime?

Asked by
Fatul 9
8 years ago

I've been trying to fix it for a few days now but I cant seem to find the reason why, much help is needed v_v.

The comments are used to seperate code for me, it's fine in the actual script.

ds = game:GetService("DataStoreService"):GetDataStore("DataTest1")
local AUTOSAVE_INTERVAL = 60
--
a = tick()
hour = math.floor(a/60/60%24)
minute = math.floor(a/60%60)
second = math.floor(a%60)
--
game.Players.PlayerAdded:connect(function(player)

    player:WaitForDataReady()
    local pKey = "plr_"..player.Name
    local dsget = ds:GetAsync(pKey)
    --  
    local function autosave()
        while wait(AUTOSAVE_INTERVAL) do
            ds:UpdateAsync(pKey, function(oldValue)
                local newValue = oldValue
                return newValue 
            end)
            print("Autosaved at " .. hour .. ":" .. minute .. " " .. second .. "s") 
        end
    end 
    --  
    local ls = Instance.new("IntValue", player)
    ls.Name = "Data"

        local Rank = Instance.new("NumberValue")
        Rank.Name = "Rank"
        Rank.Parent = ls

        local Level = Instance.new("NumberValue")
        Level.Name = "Level"
        Level.Parent = ls

        local Strength = Instance.new("NumberValue")
        Strength.Name = "Strength"
        Strength.Parent = ls

        local StrengthXP = Instance.new("NumberValue")
        StrengthXP.Name = "StrengthXP"
        StrengthXP.Parent = ls

        local Qi = Instance.new("NumberValue")
        Qi.Name = "Qi"
        Qi.Parent = ls

        local Talent = Instance.new("NumberValue")
        Talent.Name = "Talent"
        Talent.Parent = ls

        local Foundation = Instance.new("NumberValue")
        Foundation.Name = "Foundation"
        Foundation.Parent = ls

        local CoreColor = Instance.new("NumberValue")
        CoreColor.Name = "CoreColor"
        CoreColor.Parent = ls

        local Divine = Instance.new("NumberValue")
        Divine.Name = "Divine"
        Divine.Parent = ls

        local Gold = Instance.new("NumberValue")
        Gold.Name = "Gold"
        Gold.Parent = ls

        local DivineBody = Instance.new("NumberValue")
        DivineBody.Name = "DivineBody"
        DivineBody.Parent = ls

        local Sword = Instance.new("NumberValue")
        Sword.Name = "Sword"
        Sword.Parent = ls

        local Element = Instance.new("NumberValue")
        Element.Name = "Element"
        Element.Parent = ls

        local Affinity = Instance.new("NumberValue")
        Affinity.Name = "Affinity"
        Affinity.Parent = ls

        local Sense = Instance.new("NumberValue")
        Sense.Name = "Sense"
        Sense.Parent = ls

        local Border = Instance.new("NumberValue")
        Border.Name = "Border"
        Border.Parent = ls

        local TrueLevel = Instance.new("NumberValue")
        TrueLevel.Name = "TrueLevel"
        TrueLevel.Parent = ls

        local TrueRank = Instance.new("NumberValue")
        TrueRank.Name = "TrueRank"
        TrueRank.Parent = ls
        --Getting Values
        if ds:GetAsync(pKey) then
            Rank.Value = dsget
            Level.Value = dsget
            Strength.Value = dsget
            StrengthXP.Value = dsget
            Qi.Value = dsget
            Talent.Value = dsget
            Foundation.Value = dsget
            CoreColor.Value = dsget
            Divine.Value = dsget
            Gold.Value = dsget
            DivineBody.Value = dsget
            Sword.Value = dsget
            TrueLevel.Value = dsget
            TrueRank.Value = dsget
            Element.Value = dsget
            Affinity.Value = dsget
            Sense.Value = dsget
            Border.Value = dsget
                local items = {Rank.Value, Level.Value, Strength.Value, StrengthXP.Value, Qi.Value, Talent.Value, Foundation.Value, CoreColor.Value, Divine.Value, Gold.Value, DivineBody.Value,Sword.Value, TrueLevel.Value, TrueRank.Value, Element.Value, Affinity.Value, Sense.Value, Border.Value}
                ds:SetAsync(pKey, items)
        else
        --Setting Values
        if Rank.Value == 0 then
            Rank.Value = 1
        end  
        if Level.Value == 0 then
            Level.Value = 1
        end
        if Strength.Value == 0 then
            Strength.Value = math.random(1,10)
        end
        if Talent.Value == 0 then
            Talent.Value = math.random(1,5)
        end
        if Foundation.Value == 0 then
            Foundation.Value = 1
                chance1 = math.random(1,3)
                chance2 = math.random(1,6)
            if chance1 == 3 then
                Foundation.Value = 2
            end
            if chance2 == 6 then
                Foundation.Value = 3
            end
        end
        if CoreColor.Value == 0 then
            CoreColor.Value = math.random(1,5)
        end
        if Divine.Value == 0 and Qi.Value == 0 and Level.Value == 1 and Rank == 1 then
            chance = math.random(1,16)
            if chance == 13 then
                Divine.Value = 1
            end
            if chance ~= 13 then
                Divine.Value = 0
            end
        end
        if DivineBody.Value == 0 and Qi.Value == 0 and Level.Value == 1 and Rank == 1 then
            chance = math.random(1,50)
            if chance == 1 then
                DivineBody.Value = 1
            end
            if chance == 25 then
                DivineBody.Value = 2
            end
            if chance == 50 then
                DivineBody.Value = 3
            end
            if chance ~= 1 or 25 or 50 then
                Divine.Value = 0
            end
            --
            if DivineBody.Value == 3 then
                Talent.Value = 10
            end
        end
        if Element.Value == 0 then
            Element.Value = math.random(1,6)
        end
        if Affinity.Value == 0 then
            Affinity.Value = 1
                chance1 = math.random(1,2)
                chance2 = math.random(1,5)
                chance3 = math.random(1,10)
                chance4 = math.random(1,15)
                chance5 = math.random(1,25)
                chance6 = math.random(1,50)
                chance7 = math.random(1,100)
            if chance1 == 2 then
                Affinity.Value = 2
            end
            if chance2 == 5 then
                Affinity.Value = 3
            end
            if chance3 == 10 then
                Affinity.Value = 4
            end
            if chance4 == 15 then
                Affinity.Value = 5
            end
            if chance5 == 25 then
                Affinity.Value = 6
            end
            if chance6 == 50 then
                Affinity.Value = 7
            end
            if chance7 == 100 then
                Affinity.Value = 8
            end
        end
        if TrueLevel.Value == 0 then
            TrueLevel.Value = 1
        end
        if TrueRank.Value == 0 then
            TrueRank.Value = 1
        end
        if Border.Value == 0 then
            Border.Value = 1
        end
        end
        --
    spawn(autosave)
end)


game.Players.PlayerRemoving:connect(function(player) --Save on leave
    local items = {player.Data.Rank.Value, player.Data.Level.Value, player.Data.Strength.Value, player.Data.StrengthXP.Value, player.Data.Qi.Value, player.Data.Talent.Value, player.Data.Foundation.Value, player.Data.CoreColor.Value, player.Data.Divine.Value, player.Data.Gold.Value, player.Data.DivineBody.Value, player.Data.Sword.Value, player.Data.TrueLevel.Value, player.Data.TrueRank.Value, player.Data.Element.Value, player.Data.Affinity.Value, player.Data.Sense.Value, player.Data.Border.Value}
    local pKey = "plr_"..player.Name
    --
    ds:UpdateASync(pKey, items)
    print("Saved Data")
end)


game.OnClose = function(player) --Saves when last player leaves server before  shutdown
    local items = {player.Data.Rank.Value, player.Data.Level.Value, player.Data.Strength.Value, player.Data.StrengthXP.Value, player.Data.Qi.Value, player.Data.Talent.Value, player.Data.Foundation.Value, player.Data.CoreColor.Value, player.Data.Divine.Value, player.Data.Gold.Value, player.Data.DivineBody.Value, player.Data.Sword.Value, player.Data.TrueLevel.Value, player.Data.TrueRank.Value, player.Data.Element.Value, player.Data.Affinity.Value, player.Data.Sense.Value, player.Data.Border.Value}
    local pKey = "plr_"..player.Name
    --
    ds:UpdateASync(pKey, items)
    print("Saved Data")
    wait(1)
end
0
Add "Math.randomseed(tick())" at the start of the script to help with the random numbers being the same SynphonyKnight 85 — 8y
0
That helps a lot, thanks! Fatul 9 — 8y

Answer this question