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

My datastore script is not working, not ingame and not in studio (with api on)?

Asked by
FBS_8 25
4 years ago
Edited 4 years ago

Script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DrinkStats")

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "leaderstats"
    local Drinks = Instance.new("IntValue", Leaderstats)
    Drinks.Name = "Drinks"
    Drinks.Value = 0

    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        Drinks.Value = Data
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.Drinks.Value)
    print("get saved noob")
end)

1 answer

Log in to vote
0
Answered by
Komas19 34
4 years ago
Edited 4 years ago

Try this (script to put in ServerScriptService)

    ["DoMessages?"] = true; --[[ <-- Reminder of saving/loading ]]
    ["IsAnObby?"] = false; --[[ <-- Respawns the character to fix loading ]]
    saveType = {
        ["Normal?"] = true; --[[ <-- Set to false if other ]]
        ["SavingList?"] = { --[[ <-- Specific wanted admins ]]
            ["DoIt?"] = false;
            theList = {
                "Name1";
                "Name2";
                "Name3"
            }
        };
        ["BestFriends?"] = false; --[[ <-- Best friends of your's only. ]]
        ["Friends?"] = false; --[[ <-- Friends of your's only. ]]
        ["OwnerOnly?"] = false; --[[ <-- Only you have saving stats ]]
        ["Gamepass?"] = { --[[ <-- Needs a gamepass to save ]]
            ["DoIt?"] = false;
            ["GamepassId"] = 000000
        };
        ["Asset/Item?"] = { --[[ Such as a T-Shirt ]]
            ["DoIt?"] = false;
            ["AssetId"] = 000000
        }
    };
}

--[[ The rest isn't as customizable" ]]--

debrisS, playerS, datastoreS, gamepassS, marketS = 
game:GetService("Debris"),
game:GetService("Players"),
game:GetService("DataStoreService"),
game:GetService("GamePassService"),
game:GetService("MarketplaceService")

function passedPlayer(p, isLeaving)
    print(p.Name.." has passed")
    local function sendMessage(txt)
        if gameStats["DoMessages?"] then
            local message = Instance.new("Message", p.PlayerGui)
            message.Text = txt
            debrisS:AddItem(message, 2)
        end
    end
    local save, load = 
    function()
        local cDatastore, statStorage = 
        datastoreS:GetDataStore(p.Name.."Stats"),
        p:WaitForChild("leaderstats"):GetChildren()
        for i =  1, #statStorage do
            cDatastore:SetAsync(statStorage[i].Name, statStorage[i].Value)
        end
        sendMessage("Your stats have been saved.")
    end,
    function()
        local cDatastore, stats = 
        datastoreS:GetDataStore(p.Name.."Stats"),
        p:FindFirstChild("leaderstats"):GetChildren()
        for i = 1, #stats do            
            stats[i].Value = cDatastore:GetAsync(stats[i].Name)
        end
        sendMessage("Your stats have been loaded.")
    end
    local chatted = function(c)
        c = c:lower()
        for i, v in pairs(gameStats["DoChat?"]["Keywords"]) do
            if (c == v) then
                save()
            end
        end
    end
    if isLeaving then
        save()
    else
        load()
        if gameStats["DoChat?"]["DoIt?"] then
            p["Chatted"]:connect(chatted)
        end
    end
end

function passTest(p, isLeaving)
    print("Test starting")
    if gameStats["IsAnObby?"] then
        local cAdded = function(c)
            p:LoadCharacter(true)
        end
        p["CharacterAdded"]:connect(cAdded)
    end
    if gameStats.saveType["Normal?"] then
        passedPlayer(p, isLeaving)
    else
        if gameStats.saveType["SavingList?"]["DoIt?"] then
            for i, v in pairs(gameStats.saveType["SavingList?"].theList) do
                if (p.Name == v) then
                    passedPlayer(p, isLeaving)
                end
            end
        elseif gameStats.saveType["BestFriends?"] then
            if p:IsBestFriendsWith(game.CreatorId) then
                passedPlayer(p, isLeaving)
            end
        elseif gameStats.saveType["Friends?"] then
            if p:IsFriendsWith(game.CreatorId) then
                passedPlayer(p, isLeaving)
            end
        elseif gameStats.saveType["OwnerOnly?"] then
            if (p.userId == game.CreatorId) then
                passedPlayer(p, isLeaving)
            end
        elseif gameStats.saveType["Gamepass?"]["DoIt?"] then
            if p:PlayerHasPass(p, gameStats.saveType["Gamepass?"]["GamepassId"]) then
                passedPlayer(p, isLeaving)
            end
        elseif gameStats.saveType["Asset/Item?"]["DoIt?"] then
            if p:PlayerOwnsAsset(p, gameStats.saveType["Asset/Item?"]["AssetId"]) then
                passedPlayer(p, isLeaving)
            end
        end
    end
end

pAdded, pLeaving = 
function(p)
    local lStats = false
    while true do
        if p:findFirstChild("leaderstats") then
            lStats = p:findFirstChild("leaderstats")
            passTest(p, false)
            break
        end
        wait()
    end
end,
function(p)
    passTest(p, true)
end

playerS["PlayerAdded"]:connect(pAdded)
playerS["PlayerRemoving"]:connect(pLeaving)

If this won't work make sure that api is ON

Ad

Answer this question