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

How do you save multiple values to a single key in a DataStore?

Asked by
Zeluxis 100
5 years ago
Edited 5 years ago

Hi,

I’m not sure if that is the correct way to ask the question however what I’m trying to ask is how I go about creating a sort of “criminal record system” in which one player can create a report about someone, and it saves to the datastore.

However, I’m unsure how to create multiple reports for the same person. I’ve looked around on the wiki but can’t seem to find out how to do it.

I understand this isn’t a request site however I would love to be pointed in the right direction and given a source I can read up on, or an example script.

Thanks in advance.

0
maybe you want to save it in a table??? GoldAngelInDisguise 297 — 5y
0
u have to do this with discord or a trello and file the reports in there idk how tho tacotown2 119 — 5y
0
I swear ROBLOX is stopping support for that? Zeluxis 100 — 5y
0
Use a table of tables? fredfishy 833 — 5y

2 answers

Log in to vote
1
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

Lua uses tables as its data structure. They are essentially the same as any other table, with keys and values. We can assign a data store key to a table, which, in itself, contains multiple keys and values.

Tables are created with the table constructor expression, which, in it simplest form, is written as {}.

-- Declare local t and assign it to a new table.
local t = {}
-- Assign the key "foo" to value "bar".
t["foo"] = "bar"
-- Identical to t["baz"] = "qux", but syntactically sweeter.
t.baz = "qux"
-- We can use any data type as a key, except nil, and any data type as a value.
t[false] = 123
-- Get and print the value associated with certain keys.
print(t.foo) --> bar
print(t[false]) --> 123
print(t["baz"]) --> qux
-- Table fields evaluate to nil when they are not initialised.
print(t.boo) --> nil
-- We can assign nil to a table field to delete it.
t[false] = nil

In your system, each Player might have their own table of criminal records that you get and update appropriately.


If this answer solved the problem, please click "Accept Answer" to mark the question as answered and provide other users with a solution if they come across a similar issue.
Ad
Log in to vote
0
Answered by 5 years ago

I don't really know if the script below me will work. But I have a data store that I will use in the script below. Plus I am assuming tables (Roblox tables) V.S. Strings, Ints, ETC ... Work similar in datastores.

-- Make a Table, Define the DataStore

local  Record = {"I am not a criminal yet", "The game is still loading"}
local DataStore = game:GetService("DataStoreService"):GetDataStore("Example Data Store")


-- When the player joins the game then
game.Players.PlayerAdded:Connect(function(Player) -- The player who joined the game.


    local ID = "Record".."-"..Player.UserId
    local SavedData = nil



    pcall(function() -- If there is an error don't stop running the script!!! Keep going!
    SavedData = DataStore:GetAsync(ID)
    end)

    if SavedData ~= nil then -- If we have no more data to load then ... We are done :D ah ...
        Currency.Value = SavedData
        print("Data Loaded")
    else 
        Currency.Value = 0
        print("A new player has joined the game")

    end
end)
-- When the player leaves the game then save their data
game.Players.PlayerRemoving:Connect(function(Player)  -- That defines the player
    local ID = "Record".."-"..Player.UserId
    DataStore:SetAsync(ID, Record)
end)


-- If the game gets shut down then:
game:BindToClose(function() -- Notice we don't have player in there ;).

    for i, Player in pairs(game.Players:GetPlayers()) do
        if Player then
            Player:Kick("This game is shutting down, It might be updating!")

        end
    end

    wait(3) -- The clock is ticking. Make that whatever you want.

end)

Hope that helps ;D.

Answer this question