Hello everyone, I wondered if anyone could give me a sample code to save an int value in datastore and save this value when the player enters and leaves the game.
All this is as a learning, if anyone could please give me an example code of how to save an int value in the player with the value 0 I would really appreciate it very much.
What am I going to do with the value 0?
R. What I will do with the value 0 will be to test the roblox player with f9 on the console by adding values and disconnecting and re-entering the game to see if it was saved.
I’ve really seen a lot of videos and read the documentation, that’s why I need sample codes, it would really help me a lot to improve my learning, greetings.
local DataStore = game:GetService("DataStoreService"):GetDataStore("CHANGE_THIS")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Gold = Instance.new("IntValue", leaderstats) -- blah blah make your stats
-- Load data
local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = DataStore:GetAsync(key)
end)
if success then
Gold.Value = data
elseif data == nil then
Gold.Value = 0
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_".. player.UserId
local data = player.leaderstats.Gold.Value
DataStore:SetAsync(key, data)
end)
Thank you very much I really appreciate it, what happens if I don’t need a leaderstats, can I rename the folder so it’s not a leaderstats? And what should I put in CHANGE_THIS?
You can Change the name of the leaderstats folder or delete it, then just set the Value parent to player.
The “CHANGE_THIS” is just the name of the DataStore wich you can change to what ever you want.
I just changed some things in the code, is it good?
script:
local DataStore = game:GetService("DataStoreService"):GetDataStore("myData")
game.Players.PlayerAdded:Connect(function(player)
local playerFolder = Instance.new("Folder", player)
playerFolder.Name = "playerFolder"
local Gold = Instance.new("IntValue", playerFolder) -- blah blah make your stats
Gold.Name = "myGold"
-- Load data
local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = DataStore:GetAsync(key)
end)
if success then
Gold.Value = data
elseif data == nil then
Gold.Value = 0
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_".. player.UserId
local data = player.playerFolder.Gold.Value
DataStore:SetAsync(key, data)
end)
How you you make that value save among all places such as if you have a portal to another place and your shop uses the same currency as both places. Is there a way to pull that same data from the data store from the previous place?
With roblox, you have Start Places and Places.
I really cant explain this well, so I’ll just use screenshots.
(This screenshot was taken in studio, in the Asset Manager tab)
The Start Place and all other Places associated with your game share the same DataStores.
So basically, you can just copy-paste entire datastore scripts across these places and the data will transfer over between places.