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

Datastore script not loading/saving data with HTTP service?

Asked by 3 years ago
Edited 3 years ago

Ive been at this all day, and there are no errors, it just doesnt load/save. (Im not sure which one). Btw i use http service because it is easier and faster to save alot of data, in the future.

-- [ 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

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

    -- Player Data
    local UserId = Player.UserId

    local PlayerData = {
        Coins = Coins.Value,
        PlotType = PlotType.Value
    }

    local Success = false
    local Count = 10
    local CollectedData
    local DecodedData
    while Success == false and Count > 0 do
        local Suc,Err = pcall(function()
            CollectedData = Data:GetAsync(UserId)
            DecodedData = Http:JSONDecode(CollectedData)
        end)
        if Suc then
            Success = true
        else
            Count = Count-1
            warn(Err)
        end
        wait(0.5)
    end
    if Count ~= 0 then
        PlayerData.Coins = DecodedData.Coins
    end
end)
Players.PlayerRemoving:Connect(function(Player)
    local PlayerData = {
        Coins = Player.leaderstats.Coins.Value,
        PlotType = Player.PlotType.Value
    }
    local UserId = Player.UserId
    local Success = false
    local Count = 10
    local EncodedData
    while Success == false and Count > 0 do
        local Suc,Err = pcall(function()
            EncodedData = Http:JSONEncode(PlayerData)
            Data:SetAsync(UserId,EncodedData)
        end)
        if Suc then
            Success = true
        else
            Count = Count-1
            warn(Err)
        end
    end
end)

1 answer

Log in to vote
0
Answered by 3 years ago

Alright, so first off you don't want to save data with HTTP, simply because data is different when you're saving with HTTP. It's also a weird way to save it. If you want to end something to chat logs with a webhook go ahead and do HTTP:JSONEncode(webhook, datasaved) and it will send the data that got saved, obviously you have to set that up but for a data store,just use DS and Set/Get Async(). I recommend just keeping DS to be honest.

Ad

Answer this question