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

Datastore is not saving data? [PLEASE HELP]

Asked by 3 years ago
Edited 3 years ago

Hey guys, im having an issue with my datastore. So i plan on saving alot of data, and i want to do it on one key, the player UserId. To do this, i make a table and use Http Service to encode and decode it (which works, i tested it), but it appears it does not save correctly. Here is the code:

-- [ SERVICES ] --
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Http = game:GetService("HttpService")

-- [ DATASTORES ] --
local Data = DataStoreService:GetDataStore("Data")

Players.PlayerAdded:Connect(function(Player)
    -- Leaderboard
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
    -- Coins
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    -- House
    local House = Instance.new("IntValue")
    House.Name = "House"
    House.Value = 1
    House.Parent = Player

    local PlotType = Instance.new("StringValue")
    PlotType.Name = "PlotType"
    PlotType.Parent = Player

    local HouseModel = Instance.new("StringValue")
    HouseModel.Name = "HouseType"
    HouseModel.Parent = Player

    -- Player Data
    local PlayerData = {
        Coins = Coins.Value,
        PlotType = PlotType.Value
    }
    local Encode
    local Success = false
    local UserId = Player.UserId
    while Success == false do
        local suc,err = pcall(function()
            Encode = Data:GetAsync(UserId)
        end)
        if suc then
            Success = true
        else
            print("Error loading player data")
            warn(err)
        end
    end
    local Decode
    local _Success = false
    while _Success == false do
        local suc,err = pcall(function()
            Decode = Http:JSONDecode(Encode)
        end)
        if suc then
            _Success = true
        end
    end 
end)
Players.PlayerRemoving:Connect(function(Player)
    local leaderstats = Player.leaderstats
    local UserId = Player.UserId
    local SavedData = {
        Coins = leaderstats.Coins.Value,
        PlotType = Player.PlotType.Value
    }
    local Encode
    local _Suc = false
    while _Suc == false do
        local Success,Error = pcall(function()
             Encode = Http:JSONEncode(SavedData)
        end)
        if Success then
            _Suc = true
        end
    end
    local Success = false
    while Success == false do
        local suc,err = pcall(function() -- It seems to be here
            Data:SetAsync(UserId,Encode) -- This doesn't seem to run, and the code seems to stop here for some reason
        end)
        if suc then
            print("Success!")
            Success = true
        else
            print("There was an error saving data")
            warn(err)
        end
    end
end)
0
There was a similar question for about 40 minutes ago, make sure that the game isn't shutting down when you detect `PlayerRemoving` NotTheChara 191 — 3y
0
ok lemme try that iamtryingtofindname 22 — 3y
0
nope didnt work still iamtryingtofindname 22 — 3y
0
Umm, why do you need to encode and decode the Data? NotTheChara 191 — 3y
0
so i can store a bunch of values into one key to keep stuff more organized iamtryingtofindname 22 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You don't need HttpService to save Data using Leaderstats, if I were you watch this video; https://www.youtube.com/watch?v=DkYupSBUpes

I would give you the code but apparently spoonfeeding isn't allowed on Scripting Helpers!

Good Luck, have a good day! (also mark this as solved if it worked)

0
well i know i dont have to use http service but i want to because im saving alot of data and its faster with alot of data iamtryingtofindname 22 — 3y
Ad

Answer this question