Stats not saving

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

1 Like

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.

Wouldn’t a table containing the names of the values and their default values be more efficient?

If for any reason you cant do so, you can clone the values under the script into the player.

I don’t know. A guy gave me this script because mine didn’t work and it was a while ago.

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.

I didn’t even know that existed. I’ll try that out.

Okay so I tried to do that and it doesn’t work. I don’t really understand how to use datastore2 so I don’t know how to fix it.

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

Did you enable access to API Services?

1 Like

Yes I did do that. It just doesn’t work.

Are you getting any errors in your console?

No, only the errors from scripts that used my old stats script.

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.

1 Like

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)
)

Also you forgot to combine: Datastore2:Combine(“MasterKey”, “Points”)
Put this code above the playerAdded code

After the things you said I’m getting this error:

Model.MainModule:566: DataStore2() API call expected {string dataStoreName, Instance player}, got {table, Instance}

Can you show me your full code after applying all the changes?

local DataStore2 = require(1936396537)
DataStore2:Combine("MasterKey", "Points")

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)

	Points.Changed:Connect(function()
		Points.Value:Set(PointsDataStore)
	end)
end)