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

DataStore not printing any value, Did I misspell anything?

Asked by 3 years ago

I was watching AlvinBlox tutorial and usually used the Youtube comment section to find errors and such, with this video it seemed like there wasn't many people posting their issues, the video in question is here: https://www.youtube.com/watch?v=5AUP9C0rtBY&t or you can just youtube/google "How to make a Roblox Game Saving Data (4)"

I have tried some trouble shooting myself, but this is my first time working with Datastores themselves so I got confused and with Roblox giving no errors and me not understanding how to find errors by using Roblox's method: https://developer.roblox.com/en-us/articles/Datastore-Errors ( Just simply didn't understand how to do this )

yes, the game is published with API services and HTTP request.

Mainly when I edited the code around a bit it would seem to read I would always be a new player and not a returning one, so maybe the issue is around here? ( Yes I have put print statements under both of these and ran the game myself. )

Thank you for any help, I have been up for hours staring at this text lol

if player_data ~= nil then
        -- Load the players saved data
        bucks.Value = player_data
    else
        -- Player has no data to Load
        bucks.Value = defaultCash
    end

Full Code Here:

local dataStores = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
local defaultCash = 10
local playersLeft = 0   

game.Players.PlayerAdded:Connect(function(player)

    playersLeft = playersLeft + 1

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local bucks = Instance.new("IntValue")
    bucks.Name = "Bucks"
    bucks.Value = 0
    bucks.Parent = leaderstats

    player.CharacterAdded:Connect(function(character)
         character.Humanoid.WalkSpeed = 16
        character.Humanoid.Died:Connect(function()
            -- On Character deaths, this will run
            if character:FindFirstChild("GameTag") then
                character.GameTag:Destroy()
            end

            player.LoadCharacter()
        end)

    end)

    -- Data Stores

    local player_data

    pcall(function()
        player_data = dataStores:GetAsync(player.UserId.."Bucks")
    end)

    if player_data ~= nil then
        -- Load the players saved data
        bucks.Value = player_data
    else
        -- Player has no data to Load
        bucks.Value = defaultCash
    end


end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)

    pcall(function()
        dataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
        print("Saved")  
        playersLeft = playersLeft - 1
        bindableEvent:Fire()
    end)

end)

game:BindToClose(function()
    -- Triggered upon shutdown
        while playersLeft > 0 do
            bindableEvent.Event:Wait()
    end
end)

2 answers

Log in to vote
0
Answered by 3 years ago

SOLVED!

Was just about to delete it, turns out I can add my own answer.

35      pcall(function()
36          player_data = dataStores:GetAsync(player.UserId.."Bucks")
37      end)

Has to be:

35      pcall(function()
36          player_data = dataStores:GetAsync(player.UserId.."'-Bucks")
37      end)

Pay attention to how line 36 needs the Hyphen after .UserId"

why this made my code not work is something I need to figure out in the future.

Ad
Log in to vote
0
Answered by 3 years ago

I'll tell you why you need a hyphen.

Let's look at line 55:

 dataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)

Since you are saving the player's data with (player.UserId.."-Bucks") [Notice the hyphen], when you get the data, you have to include the hyphen as well.

0
Ahh I see, ty for that. I suppose for my own code projects Ill probably just avoid Hyphens, it seems like weird extra words for no reason. PointLocked 0 — 3y

Answer this question