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

Data store is not working. Sorry for so big code, im new. I think issue with loading stats?

Asked by 2 years ago

local DataStoreService = game:GetService("DataStoreService") local DataStore = game:GetService("DataStoreService"):GetDataStore("Testin") local Players = game:GetService("Players")

local failedToGet = {} local IS_BIND_TO_CLOSE = false

local function WaitForRequestBudget(requestType, requestsNeeded) local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType) while currentBudget < (requestsNeeded or 1) do currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType) wait(5) end end

local function SaveData(player, dontWaitForRequest) if not failedToGet[player.Name] then

    local success, errormessage 
    local retries = 0
    local data = {
        player.Strength.Value,
        player.Tokens.Value,
        player.Endurance.Value,
        player.Psp.Value,
        player.Agility.Value,
        player.Multipliers.Multi.Value,
        player.Multipliers.eMulti.Value,
        player.Multipliers.pMulti.Value,
        player.Multipliers.aMulti.Value,
        player.TotalPower.Value,
        player.Powers.hasFly.Value,
        player.Powers.hasEnergyPunch.Value,
        player.Powers.hasInvisability.Value,
        player.Class.Value,
        player.leaderstats.Killstreak.Value,
        player.leaderstats.Status.Value,
        player.Reputation.Value,
    }

    repeat
        if not dontWaitForRequest then --I use this so the requestbudget function doesnt yield when a server is shutting down, there is only 30 seconds to save every players data
            WaitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
        end

        success, errormessage = pcall(function()
            DataStore:UpdateAsync(player.UserId, function(data)
                -- Only save data if it was updated since it was loaded
                return data ~= data and data or nil
            end)

            retries += 1
        end)

    until success or retries >= 5

    if retries >= 5 then
        warn(("Error saving %s's data: %s"):format(player.Name, errormessage))
    end
end

failedToGet[player.Name] = nil

end

Players.PlayerAdded:Connect(function(player)

local stats = Instance.new("Folder", player)
stats.Name = "leaderstats"

local Multis = Instance.new("Folder", player)
Multis.Name = "Multipliers"

local Lbs = Instance.new("Folder", player)
Lbs.Name = "Leaderboard"

local Title = Instance.new("StringValue", stats)
Title.Name = "Status"
Title.Value = "Innocent"

local Streak = Instance.new("IntValue", stats)
Streak.Name = "Killstreak"
Streak.Value = 0

local Rep = Instance.new("NumberValue", player)
Rep.Name = "Reputation"
Rep.Value = 0

local Class = Instance.new("StringValue", player)
Class.Name = "Class"
Class.Value = "F-Class"

local Tokens = Instance.new("IntValue", player)
Tokens.Name = "Tokens"
Tokens.Value = 1e6

local strength = Instance.new("NumberValue", player)
strength.Name = "Strength"
strength.Value = 0

local quest = Instance.new("NumberValue", player)
quest.Name = "Quest"
quest.Value = 0

local lbstrength = Instance.new("NumberValue", Lbs)
lbstrength.Name = "LBStrength"
lbstrength.Value = 0

local agility = Instance.new("NumberValue", player)
agility.Name = "Agility"
agility.Value = 0

local psp = Instance.new("NumberValue", player)
psp.Name = "Psp"
psp.Value = 0

local lbpsp = Instance.new("NumberValue", Lbs)
lbpsp.Name = "LBPsp"
lbpsp.Value = 0

local Mult  = Instance.new("IntValue", Multis)
Mult.Name = "Multi"
Mult.Value = 1

local eMult  = Instance.new("IntValue", Multis)
eMult.Name = "eMulti"
eMult.Value = 1

local pMult  = Instance.new("IntValue", Multis)
pMult.Name = "pMulti"
pMult.Value = 1

local aMult  = Instance.new("IntValue", Multis)
aMult.Name = "aMulti"
aMult.Value = 1

local zoneMult = Instance.new("IntValue", player)
zoneMult.Name = "ZoneMulti"
zoneMult.Value = 1

local pZoneMult = Instance.new("IntValue", player)
pZoneMult.Name = "pZoneMulti"
pZoneMult.Value = 1

local aZoneMult = Instance.new("IntValue", player)
aZoneMult.Name = "aZoneMulti"
aZoneMult.Value = 1

local End = Instance.new("NumberValue", player)
End.Name = "Endurance"
End.Value = 0

local lbEnd = Instance.new("NumberValue", Lbs)
lbEnd.Name = "LBEndurance"
lbEnd.Value = 0

local TotalPower = Instance.new("NumberValue", player)
TotalPower.Name = "TotalPower"
TotalPower.Value = strength.Value + psp.Value + End.Value + agility.Value

local powers = Instance.new("Folder", player)
powers.Name = "Powers"

local hasAura = Instance.new("BoolValue", powers)
hasAura.Name = "hasAura"
hasAura.Value = false

local hasFly = Instance.new("BoolValue", powers)
hasFly.Name = "hasFly"
hasFly.Value = true

local hasEnergyPunch = Instance.new("BoolValue", powers)
hasEnergyPunch.Name = "hasEnergyPunch"
hasEnergyPunch.Value = true

local IsInvisible = Instance.new("BoolValue", player)
IsInvisible.Name = "IsInvisible"
IsInvisible.Value = false

local hasInvisability = Instance.new("BoolValue", powers)
hasInvisability.Name = "hasInvisability"
hasInvisability.Value = true

local ctrl = Instance.new("BoolValue", player)
ctrl.Name = "Ctrl"
ctrl.Value = false


player.CharacterAdded:connect(function()
    local hum = player.Character:WaitForChild("Humanoid")
    if hum then
        hum.MaxHealth = (End.Value + 100) * 10
        hum.Health = hum.MaxHealth * 0.7
    end
end)


local success, errormessage 
    success, errormessage = pcall(function()
        WaitForRequestBudget(Enum.DataStoreRequestType.GetAsync)

    local data = DataStore:GetAsync("Player_"..player.UserId)

    if data then
        player.Strength.Value = data[1]
        player.Tokens.Value = data[2]
        player.Endurance.Value = data[3]
        player.Psp.Value = data[4]
        player.Agility.Value = data[5]
        player.Multipliers.Multi.Value = data[6]
        player.Multipliers.eMulti.Value = data[7]
        player.Multipliers.pMulti.Value = data[8]
        player.Multipliers.aMulti.Value = data[9]
        player.TotalPower.Value = data[10]
        player.Powers.hasFly.Value = data [11]
        player.Powers.hasEnergyPunch.Value = data[12]
        player.Powers.hasInvisability.Value = data[13]
        player.Class.Value = data[14]
        player.leaderstats.Killstreak.Value = data[15]
        player.leaderstats.Status.Value = data[16]
        player.Reputation.Value = data[17]
        player.Leaderboard.LBStrength.Value = math.floor(player.Strength.Value / 1e12)
        player.Leaderboard.LBPsp.Value = math.floor(player.Psp.Value / 1e12)
        player.Leaderboard.LBEndurance.Value = math.floor(player.Endurance.Value / 1e12)
    end
end)

local Player = game:GetService("Players").LocalPlayer
strength.Changed:Connect(function()
    TotalPower.Value = strength.Value + psp.Value + End.Value
end)

psp.Changed:Connect(function()
    TotalPower.Value = strength.Value + psp.Value + End.Value
end)

End.Changed:Connect(function()
    TotalPower.Value = strength.Value + psp.Value + End.Value
end)

Rep.Changed:Connect(function()
    if Rep.Value == 0 then
        Title.Value = "Innocent"
    elseif Rep.Value >= 1 and Rep.Value < 5 then
        Title.Value = "Protector"
    elseif Rep.Value >= 5 and Rep.Value < 20 then
        Title.Value = "Guardian"
    elseif Rep.Value > 19 then
        Title.Value = "Superhero"
    elseif Rep.Value <= -1 and Rep.Value > -5 then
        Title.Value = "Lawbreaker"
    elseif Rep.Value <= -5 and Rep.Value > -20 then
        Title.Value = "Criminal"
    elseif Rep.Value < -19 then
        Title.Value = "Supervillain"
    end
end)

end)

Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function() IS_BIND_TO_CLOSE = true

for _, player in ipairs(Players:GetPlayers()) do
    coroutine.wrap(SaveData)(player, true)
end

end)

while not IS_BIND_TO_CLOSE do wait(10)

for _, player in ipairs(Players:GetPlayers()) do
    if IS_BIND_TO_CLOSE then -- dont save data if server is about to shutdown
        return
    end

    coroutine.wrap(SaveData)(player)
end

end

0
the formatting tho... Xyternal 247 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
-- Change this
local data = DataStore:GetAsync("Player_"..player.UserId)

-- To this

local data = DataStore:GetAsync(player.UserId)

Your key wasn't matching.

Ad

Answer this question