I’m terrible at scripting and I could never get my head around datastores, arrays or tables.
I want to save a value to a player (BoolValue, NumberValue, etc) but I don’t know how. This has honestly been bugging me for ages on how to do this.
I’m terrible at scripting and I could never get my head around datastores, arrays or tables.
I want to save a value to a player (BoolValue, NumberValue, etc) but I don’t know how. This has honestly been bugging me for ages on how to do this.
local DataStoreService = game:GetService("DataStoreService") -- Get the data store
local playerDataStore = DataStoreService:GetDataStore("playerDataStore") -- create a data store the name doesnt matter
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder") -- create a leaderstats
local Money = Instance.new("NumberValue")
local Exp = Instance.new("NumberValue")
leaderstats.Name = "leaderstats"
Money.Name = "Money"
Exp.Name = "Exp"
leaderstats.Parent = player
Money.Parent = leaderstats
Exp .Parent = leaderstats
local success, errormsg = pcall(function() -- a pcall will run even if the script has an error and you can check if it is an error with errormsg or success but the name doesn't matter
local data = playerDataStore:GetAsync(player.UserId) -- Grab the saved data (Also the player.userId is the password for the datastore)
if data[1] ~= nil then -- Check if there is saved data / check if the first value exists (not nil)
Money.Value = data[1]
end
if data[2] ~= nil then
Exp.Value = data[2]
end
end)
game.Players.PlayerRemoving:Connect(function(leavingPlayer)
if leavingPlayer.userId == player.UserId then -- Check if the leaving player is the same player that joined so we can save their data correctly
playerDataStore:SetAsync(player.UserId, { -- Save it to a table (Something that can hold a bunch of stuff basically which in this case is 2 different values)
Money.Value, -- Save the money value to the first value in the table
Exp.Value -- Save the exp value to the first value in the table
})
end
end)
end)
Sorry if this isn’t very helpful but if you don’t understand it try looking up tables and datastores
I recommend using DataStore2 as it is a lot more understandable than the standard DataStore.
Dont rush with it, you simply need undestand datastores logic.
arrays isnt that hard as you think. Again, just understand logic of programming.
1. Arrays
local sheesh = {"oh"}
local array = {"apple",5,bool,sheesh} -- yes we can insert table in to a table
print(array[1]) -- would print 'apple' in lua tables first index its 1
print(array[4][1]) -- kinda complicated huh? its simple tho. 4 indexed thing its our 'sheesh' table, so we doing similiar to sheesh[1], so it would print 'oh', because its first in 'sheesh' table
-- Basically we can put almost everything in tables and use them as we want
-- we also can use strings as indexes
local dictionary = {} -- dictionaries its like arrays but instead of numbers in index we using other things, strings for example.
dictionary["how"] = 250
print(dictionary.how) -- 250
print(dictionary["how"]) -- 250
2. Datastores
Basically datastores its just cloud saving in roblox.
to get our Storage from that cloud we using
local DataStoreService = game:GetService("DataStoreService")
local PlayersData= DataStoreService:GetDataStore("PlayersData")
-- Players data its HUGE "array", that contains keys (as indexes in arrays) and values for that keys.
-- To get Value we need do this
local valueFromStorage = PlayersData:GetAsync("OurKeyHere")
-- To set Value we can do this
PlayersData:SetAsync("OurKeyHere",something)
--we can set ONLY STRINGS, NUMBERS, TABLES, BOOLEANS
-- YOU CANT SAVE OBJECTS/INSTANCES
Usefull links:
How DataStores works
How Tables works
If all you want to do is save values to a player instance without saving or it appearing, use attributes.
Player:SetAttribute("Cash", 100)
Player:GetAttribute("Cash") --> 100
I recommend that if the data you’re trying to store doesn’t contain decimals (or only whole numbers ex. 10,) Then you should consider using IntValue instead of NumberValue.
Watch TheDevKing Tutorials, it’s fairy easy to understand and there’s nothing hard in it.
Any Particular one you’d reccomend?