Hey there!
So I made a stat save and load script and it doesn’t work. I tried it in game and not in studio and it still doesn’t work.
local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(player)
for i,v in pairs(script:GetChildren()) do
local d = DataStoreService:GetDataStore(v.Name)
local x = Instance.new("NumberValue", player)
x.Name = v.Name
x.Value = d:GetAsync(player.UserId) or v.Value
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(script:GetChildren()) do
print("Getting")
local d = DataStoreService:GetDataStore(v.Name)
d:SetAsync(player.UserId, player[v.Name].Value)
print("Saved")
end
end)
Gosh this is so confusing, and a very inefficient way of making a datastore I’ll tell you that much, could you tell us more of what you’re trying to do exactly? Eg what data you’re trying to save, where is the data located, etc. As much detail as you can
Okay so, there are number values inside of the script and those are the values I am trying to save. The values will be inside the player and not leaderstats because I’m showing it only in a TextLabel.
I think your best option now is datastore2 its secure and whenever i cant get my datastore to work i just use it, its foolproof and secure, no compromise.
There is a tutorial on youtube which shows you can basically just hook a .changed event up to the value and set the value whenever it changes instead of the complicated remote events. I wish i was on my laptop to show you the video
Note: do not worry about reaching the datastore limit as it will just cache the data and save it only when you leave
This is a really inefficient way of doing this use attributes or tables atleast. and also datastores tend to fail so wrap it in a pcall so you can check if it gave an error or not.
and you know servers do get shutdown so use game:bindtoclose to account for server shutdowns.
This is what I got right now, I tried following a tutorial but it doesn’t really work:
local DataStore2 = require(1936396537)
local DefaultValue = 0
game.Players.PlayerAdded:Connect(function(player)
local PointsDataStore = DataStore2("Points", player)
local Points = Instance.new("IntValue", player)
Points.Name = "Points"
local function PointsUpdate(UpdatedValue)
Points.Value = PointsDataStore:Get(UpdatedValue)
end
PointsUpdate(DefaultValue)
PointsDataStore:OnUpdate(PointsUpdate)
end)
Try this:
If PointsDatastore:Get ~= nil then
Points.Value = PointsDatastore:Get()
End
Points.Changed:Connect(function()
Points.Value:Set(PointsDatastore)
)