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

Why do the coins and checkpoints only save the first time a player joins the game?

Asked by
Soban06 410 Moderation Voter
4 years ago

So I am making an obby in Roblox. I have a data store for saving the leaderboard value which are coins and checkpoints. The Data Store I have is in the ServerScriptService.

The problem is that when the player joins the game for the first time, and they for example complete the first 5 levels of the game and they get 2 coins. The next time they join the game they are still on level 5 and they still have 2 coins from the previous game. But if they now play the game(i.e. they login to the game the second time) and let's say they reach level 10 and have 4 coins, the process should go like that if the player now leaves the game, and when they rejoin, they should be on level 10 and should have 4 coins.

But what happens is that on they are not on level 10 and they don't have 4 coins, they are on still on level 5 and their coins are 2. Just as their progress was on their first play.

Why doesn't this data store update the values on the leader board the third time the player log's in the game? And so on?

Here's the script which holds the whole data store which is located in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


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

    local Checkpoint = Instance.new("IntValue", leaderstats)
    Checkpoint.Name = "Checkpoint"
    Checkpoint.Value = 1

    local coins = Instance.new("IntValue", leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    --Checkpoint Section
    player.CharacterAdded:Connect(function(character)

        repeat wait() until player.character ~= nil
        local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value)
        character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 2, 0))

    end)

    -- Data Store Section

    local playerUserId = "Player_"..player.UserId
    print(playerUserId)

    -- Load Data

    local data
    local success, errormessage = pcall(function()
         data = myDataStore:GetAsync(playerUserId)

    end)


    if success then
        if data then
        coins.Value = data.Coins
        Checkpoint.Value = data.Checkpoint
        -- Set our data equal to the current Coins
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = {

    Coins = player.leaderstats.Coins.Value;
    Checkpoint = player.leaderstats.Checkpoint.Value;

    }

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

        if success then
            print("Data successfully saved!")
        else
            print("There was an error saving the data!")
            warn(errormessage)  
        end

end)

Please do help as I really need the answer.

Thanks

1 answer

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

I think the problem is at line 42 and 43. You used data.Coins and data.Checkpoint when you should use data[1] and data[2]

Data[1] is the same as data.Coins

Data[2] is the same as data.Checkpoint

Also, I don't think you should make Coins and Checkpoint into a variable as seen on line 55 and 56.

0
The how should I make the Coins and Checkpoint? Soban06 410 — 4y
0
What do you mean by how should I make it? Like how will it get the data? youtubemasterWOW 2741 — 4y
0
I mean that here I made variables for Coins and Checkpoints. You are saying that do not make the variables. then should I just say "player.leaderstats.Coins.Value" etc. Soban06 410 — 4y
0
Try doing this: local data = {player.leaderstats.Coins.Value; player.leaderstats.Checkpoint.Value;} youtubemasterWOW 2741 — 4y
Ad

Answer this question