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

What are datastores and what do they do?

Asked by 5 years ago

What is a datastore and how do i use it and is it needed for a game? Please help!

0
googling that is so hard wow lucldIy 3 — 5y

1 answer

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

A datastore is what you use to save things. Example; Say I want to save levels in a game I'd first create a folder and parent it to something. Then I'd create an instance.new of a intvalue for the level and put it inside the folder. Here's an example that would load on join and save on leave.

local player = game:GetService("Players")
local SS = game:GetService("ReplicatedStorage")
local DS = game:GetService("DataStoreService")

-- Creating the local Data makes a new table for the data to go into

local data = {
["Level"] = DS: GetDataStore("Level")
}

player.PlayerAdded:connect(function(plr) -- Function to see a player join

local success, err = pcall (function() -- pcall to secure the data
local userID = plr.userId

local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = plr -- Creates new Folder and makes it go into the player

local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = stats -- Creates the IntValue Level and puts it in stats
Level.Value = data["Level"]:GetAsync(userID) or "1" -- Syncs the user's Level to saved stat or if none sets it to 1
end)
if success then print ("Success") -- Successful pcall
end
end)

player.PlayerRemoving:connect(function(plr)
local userID = plr.userId
local stats = plr:WaitForChild("Stats") -- get the stat folder
data["Level"]:SetAsync(userID, stats.Level.Value) -- Syncs the player's current level to the Level table, causing it to save.
end)

Hoped this made sense or helped, I'm kind of new to scripting too

0
Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! turquoise_monkeyman 32 — 5y
0
Will it work with cash! turquoise_monkeyman 32 — 5y
0
It works with any Stringvalues, and IntValues DesiredRep 75 — 5y
0
stats is depreceated turquoise_monkeyman 32 — 5y
View all comments (7 more)
0
Desired #2084 add me and DM me your script DesiredRep 75 — 5y
0
The script dosent work turquoise_monkeyman 32 — 5y
0
yes it does i even tested it DesiredRep 75 — 5y
0
ok ill check once more turquoise_monkeyman 32 — 5y
0
Where does a datastore go turquoise_monkeyman 32 — 5y
0
I put it in the workspace!!!! turquoise_monkeyman 32 — 5y
0
serverscriptservice DesiredRep 75 — 5y
Ad

Answer this question