Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What are datastores and what do they do?

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

1 comment

0

googling that is so hard wow

1 answer

✓ Accepted answer

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

11 comments

0

Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0

Will it work with cash!

0

It works with any Stringvalues, and IntValues

0

stats is depreceated

0

Desired #2084 add me and DM me your script

0

The script dosent work

0

yes it does i even tested it

0

ok ill check once more

0

Where does a datastore go

0

I put it in the workspace!!!!

0

serverscriptservice

This discussion is preserved as it was. New answers, comments, and votes are closed.