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

[SOLVED] Saving & Loading strings in tables?

Asked by 4 years ago
Edited 4 years ago

Yes, there are numerous threads on this on here, and yes I've looked at about all off them but none of them are in the same situation as mine.

In my situation, the strings are being made with specific names as the player is playing. I've just got a folder in workspace called Inventory with folders in it with the names of each player online. Whenever something gets added a string gets made & added to the players folder with the correct name. All I need to save & load are the names of each of the strings, not the values.

I'm an absolute noob at datastores and I just don't fully get this. I've looked at a crap ton of wiki pages, scriptinghelpers posts, a reddit post and other things and adapted my code but I just don't fully get it just yet. None of them explain in good detail what I'm confused at.

Here's what I have,

local Players = game:GetService("Players")
local ServerStorage = game:GetService("Workspace")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Inventory")

Players.PlayerAdded:Connect(function(player)
    print("Player")
    local folder = Instance.new("Folder")
    folder.Parent = ServerStorage.Inventory
    folder.Name = (player.Name)
    local PlayerData = DataStore:GetAsync(player.UserId)
    for i, v in pairs(PlayerData) do
        local value = Instance.new("StringValue")
        value.Name = v
        value.Parent = folder
    end 

end)

local function playerLeaving(player)
        local inv = {}

        for i, v in pairs(ServerStorage.Inventory:FindFirstChild(player.Name):GetChildren()) do
        table.insert(inv, v.Name)
        end 


    DataStore:SetAsync(player.UserId, inv)


    ServerStorage.Inventory:FindFirstChild(player.Name):Destroy()
end

game.Players.PlayerRemoving:Connect(playerLeaving)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        playerLeaving(player)
    end
end)

When the player rejoins it supposedly should take all of the string values out of the table and create strings for each one renamed to the appropriate title and put in the players folder.

Not really getting any errors, it just doesn't work.

However, I am getting this in console

17:22:03.523 - DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = (an 8 digit string of numbers)

I'm mostly confused at loading all of the values when the player joined. Haven't seen anyone explain that really anywhere.

EDIT : I fixed 1 wrong line, tried in a live-game, still doesn't work. However, I added a print between lines 15 & 16 that looks like

print(v.Name.." Loaded")

And it comes up as attempt to concatenate nil with string

0
Does this issue happen in both studio and in-game? It appears to me like your code is fine. In studio, both BindToClose and PlayerRemoving fire at the same time. Try testing this in a live, in-game environment. Fifkee 2017 — 4y
1
you forgot to set value.Parent at line 14 Leamir 3138 — 4y
0
Thankyou for pointing that out, however it still doesn't work barrettr500 53 — 4y
0
It doesn't appear to work in a live game either barrettr500 53 — 4y
0
After some more fiddling, I might have it solved. Gonna mess with it some more, if I get it I'll make an answer to this post to help anyone else who finds this barrettr500 53 — 4y

1 answer

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

I added 1 correction edited onto main question and was told it would work if I tried it in a main-server game, which was true for some bits, it really just needed some fiddling.

Ad

Answer this question