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:
local DataStore2 = require(1936396537)
alright, we got the API, we now need to make the default values, write this:
local DataStore2 = require(1936396537)
local Points_Default = 0
local Gold_Default = 0
now, we need to start the datastores, so write this
local DataStore2 = require(1936396537)
local Points_Default = 0
local Gold_Default = 0
game.Players.PlayerAdded:Connect(function(LocalPlayer)
end)
in the start, we need to write the datastores, some functions and the values
local DataStore2 = require(1936396537)
local Points_Default = 0
local Gold_Default = 0
game.Players.PlayerAdded:Connect(function(LocalPlayer)
local Leaderstats = Instance.new("Folder",LocalPLayer)
Leaderstats.Name = "leaderstats"
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"
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
Thank you for the help much appreciated and a wonderful explanation.
and i forgot what is the updatevalue for