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

Can't get DataStores to save across places?

Asked by 3 years ago

I'm very new to DataStores and I've been trying to figure them out all day. I watched a tutorial and stared at the DevHub page for a while, but can't seem to pinpoint what I've done wrong.

In my game, there is a lobby with 50 player max and a level with 1 player max. After the level is complete it sets your value to 1 if it was at 0 before, but then when you go back to the lobby the 1 doesn't seem to be saving over.

And yes these are places inside the same game.

Here is the lobby script which is in ServerScriptService

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local levelDataStore = DataStoreService:GetDataStore("levelDataStore")


game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local levelVal = Instance.new("IntValue")
    levelVal.Name = "LevelPlayer"
    levelVal.Parent = leaderstats

    local data = {}

    local success, errormessage = pcall(function()
        data = levelDataStore:GetAsync(player.UserId.."-level") --.."-level"
    end)

    if success then
        print("Successfully imported data.")
        levelVal.Value = data
    else
        print("There was an error importing your data.")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        levelDataStore:SetAsync(player.UserId.."-level",player.leaderstats.levelVal.Value)--.."-level"
    end)

    if success then
        print("Player data successfully saved.")
    else
        print("There was an error saving your data.")
        warn(errormessage)
    end
end)

game.ReplicatedStorage.PlayerLevelReset.OnServerEvent:Connect(function(player)
    local success, errormessage = pcall(function()
        levelDataStore:UpdateAsync(player.UserId.."-level",function(level)
            local newValue = 0
            return newValue
        end)
    end)
    if success then
        print("Data reset successfully.")
    else
        print("There was an error reseting your data.")
        warn(errormessage)
    end
end)

Here is the level script which is also in ServerScriptService

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local levelDataStore = DataStoreService:GetDataStore("levelDataStore")

local goldButton = game.Workspace.EscapeElevator.GoldButton.ClickDetector
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local levelVal = Instance.new("IntValue")
    levelVal.Name = "LevelPlayer"
    levelVal.Parent = leaderstats

    local data = {}

    local success, errormessage = pcall(function()
        data = levelDataStore:GetAsync(player.UserId.."-level") --.."-level"
    end)

    if success then
        print("Successfully imported data.")
        levelVal.Value = data
    else
        print("There was an error importing your data.")
        warn(errormessage)
    end
    local function giveData()
        if levelVal.Value == 0 then
            levelVal.Value=1
            print("Value added")
        else
            print("Already at this level!")
        end
    end
    goldButton.MouseClick:Connect(giveData)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        levelDataStore:SetAsync(player.UserId.."-level",player.leaderstats.levelVal.Value)--.."-level"
    end)

    if success then
        print("Player data successfully saved.")
    else
        print("There was an error saving your data.")
        warn(errormessage)
    end
end)

Help would be greatly appreciated.

1 answer

Log in to vote
0
Answered by 3 years ago

I have figured out the issue after forever.

When I was doing :SetAsync I would do player.leaderstats.levelVal.Value

I was supposed to do player.leaderstats.LevelPlayer.Value because that was what I actually named the IntValue. Simple mistake, hopefully this helps someone relook over their code for an error like mine.

Ad

Answer this question