— Fixed thank you guys for checking this —
i recommend you using datastore2 by kampfkarren because it is much easier and prevent data loss
Could you provide script… im new
sorry if it gives an error, i dont type it with autofill
kampfkarren datastore2 on github
click here
local DS2 = require(path.to.datastore2)
DS2.Combine("MK1", "MyStats")
function Give(Value, Player)
local Data = DS2("MyStats", Player)
Data:Increment(Value)
end
game.Players.PlayerAdded:Connect(function(p)
local Data = DS2("MyStats", p)
print(Data:Get(0))
end)
game.Players.PlayerRemoving:Connect(p)
local Data = DS2("MyStats", p)
local s,m = pcall(function()
Data:Save()
end)
if not s then
Data:SaveAsync()
end
end)
Datastores wont work in Studio unless you have API Services Enabled for Studio in settings.
If this doesnt work, can you post the actual code instead of an image since its hard to read. Like this
will turn into
-- code in here
If your trying your code in studio it will often not work as sometimes the data is first saved after the script gets stop by the game stopping
Here is my script for saving using datastore2
local Datastore2 = require(script.Parent.DataStore2) -- path to the datastore2 module
local loadInStudio = game:GetService("ServerStorage").LoadInStudio.Value -- this is just if you want to make it so you don't load it in studio. Delete it if you want
game.Players.PlayerAdded:Connect(function(player)
local data = Datastore2("data", player)
local stats = Instance.new("Folder", player)
stats.Name = "leaderstats"
local wins = Instance.new("IntValue", stats)
wins.Name = "wins"
wins.Value = 0
local function dataUpdate(newTable)
local main = data:GetTable(newTable) -- gets stored data equals newTable if no data was stored
if main["wins"] then -- checking if wins exists
wins.Value = main["wins"]
end
end
if loadInStudio then -- this is just if you want to make it so you don't load it in studio. Remove this if you deleted the variable up at the top
dataUpdate({
["wins"] = 0, -- defualt value to use if no data is stored
})
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = Datastore2("data", player)
local main = {}
main["wins"] = player.stats.wins.Value
data:Set(main)
end)
Datastore2 module: Release DataStore2 v1.3.0 · Kampfkarren/Roblox · GitHub
Ignoring the idiocy of DataStore2 recommendations as a solution, append
game:BindToClose(function()
wait(20);
end);
to the end of your script. This will prevent the game from closing before data is saved.
well i would say that is a solution but i would say it is bad thing to do it is just easier to use datastore2 all together as it always finishes before the game closes so it is better to use. But sure for normal datastore it will work
i am not saying he is using it or going to use it i am just saying that if he choses to use normal datastore it would be a solution but if he uses datastore2 which is recommended instead it isn’t a good idea to use that
The leaderstats should be called “leaderstats”.
just so you know it can be anything it is only if you want it to show the values in the top right corner of the screen
What does it print out when it warns?
I might be missing something here, but local data has nothing after it therefore it causes nothing, might be wrong though
(Post withdrawn by the author, it will be automatically deleted)
I recommend starting a separate script to work on some functions then copying the functions over whenever you need them. This way you will be able to compare them to see where you went wrong and also allow you to improve things.
I’ve seen many direct you to DataStore2, it is an alright system to use if you can tolerate the functions. DSS2 has been around for a while so is a rather developed script but they have still overlooked the core principle of the DataStoreService.
However, I recommend making your own set of functions that you can use on anything that uses datastores. This way you can get it to do what you want in your way. I’ve got my own functions I use and as a result I find it better than using anyone else’s script but this isn’t for me to explain what I have created, just a bit of advice.
Something to keep in mind.
-
DSS is designed to save either text or numbers originally so text is most compatible in terms of what you can do with it. See String API
-
The API will explain everything about the core principles, however they keep it simple so its somewhat open to interpretation (How you interpret it will determine how good your script is). See DSS API
-
Saves to a location with two variables that determine the save name (StoreName, SaveIdentifier) both of which are made up of string (The string can be made by other scripts, this is a useful fact)
leaderstats.Name = "leaderstats"
Your leaderstats folder should have the name leaderstats. Anything else will cause the leaderboard not to display.