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

how to make a data store for the leaderstats ?

local DSS = game:GetService("DataStoreService")

local Playerstats = DSS:GetDataStore("PlayerStats")

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

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = player

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

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

local data 

local success, errormessage = pcall(function()

	local data = Playerstats:GetAsync(player.UserId..("-gold"))

end)


if success then


	player.leaderstats.Gold.Value = data

else

	print("An Error Appearred while retriving data")

	warn(errormessage)

end

end)

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

local success, errormessage = pcall(function()

	Playerstats:SetAsync(player.UserId..("-gold"),player.leaderstats.Gold.Value)

end)


if success then 

	print("Player Data Successfully Saved")

else

	print("Error in Saving Player Data")


	warn(errormessage)

end

end)

can anyone verify if this script is working because i already turned on api and nothing happens .

1 answer

✓ Accepted answer

I use datastore2 API which is a module script which simply helps you with your datastores.. well... it's as if the service didn't exist and instead were a lot of code.. good part is that the data saves when the player leaves.. so to start, add a script in the server script service, then on the script add:

--·DataStore·--
local DataStore2 = require(1936396537)

alright, we got the API, we now need to make the default values, write this:

--·DataStore·--
local DataStore2 = require(1936396537)

--Default Values
local Points_Default = 0
local Gold_Default = 0

now, we need to start the datastores, so write this

--·DataStore·--
local DataStore2 = require(1936396537)

--Default Values
local Points_Default = 0
local Gold_Default = 0

--Start
game.Players.PlayerAdded:Connect(function(LocalPlayer)--I like to call it LocalPlayer
	
end)

in the start, we need to write the datastores, some functions and the values

--·DataStore·--
local DataStore2 = require(1936396537)

--Default Values
local Points_Default = 0
local Gold_Default = 0

--Start
game.Players.PlayerAdded:Connect(function(LocalPlayer)--I like to call it LocalPlayer
	--Main data folder / leaderstats
	local Leaderstats = Instance.new("Folder",LocalPLayer)
	Leaderstats.Name = "leaderstats"
	
	--DataStores
	local PointsDataStore = DataStore2("Points",LocalPlayer)
	local Points = Instance.new("IntValue",Leaderstats)
	Points.Name = "Points"
	
	local GoldDataStore = DataStore2("Gold",LocalPlayer)
	local Gold = Instance.new("IntValue",Leaderstats)
	Gold.Name = "Gold"
	
	--Loading functions
	local function PointsUpdate(UpdatedValue)
		Points.Value = PointsDataStore:Get(UpdatedValue)
	end
	local function GoldUpdate(UpdatedValue)
		Gold.Value = PointsDataStore:Get(UpdatedValue)
	end
	
	PointsUpdate(Points_Default)
	GoldUpdate(Gold_Default)
	
	PointsDataStore:OnUpdate(PointsUpdate)
	GoldDataStore:OnUpdate(GoldUpdate)
end)

after that, you can add the functions to update the values, for that you must get a function which returns the player, for example a remote event, we can write this after the end)

game.ReplicatedStorage.UpdateValue.OnServerEvent:Connect(function(LocalPlayer,Gold_Points,value,Increment_Set)
	if Gold_Points == "Gold" then
	local GoldDataStore = DataStore2("Gold",LocalPlayer)
		if Increment_Set == "Set" then
		GoldDataStore:Set(value)
		else
		GoldDataStore:Increment(value)
		end
	else
	local PointsDataStore = DataStore2("Points",LocalPlayer)
		if Increment_Set == "Set" then
		PointsDataStore:Set(Value)
		else
		PointsDataStore:Increment(Value)
		end
	end
end)

oh and if you want it to save on studio, make a bool value on the server storage, set it true and name it: "SaveOnStudio"

2 comments

0

Thank you for the help much appreciated and a wonderful explanation.

0

and i forgot what is the updatevalue for

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