How do I use datastore and save player stand? I can’t find a way to save the stand since it was a word not number value
What do you mean by “save player stand” ?
Im making a stand game I want to make it save when player leave or die
Save the name of the stand into a datastore. Make sure when the player rejoins it checks the name of the stand to know which stand the player currently has.
For an example to save a stand the datastore must see it as:
StandUserDataStore:
Key = JojogamezYT, Value = Star Platinum: The World (Stand Name/string)
If there is something wrong in my answer, please let me know immediately as I am recently learning how to use data warehousing:
The DataStore is one of the most impotant things in a game, and it is necessary to learn it and that is why I will now give you information on how to use it.
The DataStore is a non-creatable service with the ``Instance.new()` and is obtained in the following way:
local Data = game:GetService("DataStoreService")
With that we already acquired the Service, now we need the store and with our own key, for that we use the method “:GetDataStore()”.
local Data = game:GetService("DataStoreService")
local Storage = Data:GetDataStore("MyDataStore")
With this we have now created our own data store.
Once the data store is created, we must continue to save the data and to obtain them. For that I will create a folder called “leaderstats” and with “values” as children of the parent folder “leaderstats”. This must be done once the player finishes joining the game, it is important, the event “PlayerAdded” is fired when the player finishes joining the game.
local Data = game:GetService("DataStoreService")
local Storage = Data:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "leaderstats".
local Currency = Instance.new("IntValue", Folder)
Currency = "Point" -- Name of the player's currency
end)
And with this we already have what would be the “player data reflection” but we still need to obtain and store the data, for that there are 2 methods and they are:
GetAsync() – To obtain the data by means of a key
SetAsync() – To store the data by means of a key.
so first let’s start with the data saving, for that we use SetAsync()
to save and BindToClose()
to make sure that the player’s data is saved ( but this doesn’t take away from avoiding data loss, I recommend using HTTPService and mongoDB to make your own DataStore and avoid data loss ). You should also use pcall to know if the data was saved correctly or maybe not because of internet problems. We will do it like this:
local Data = game:GetService("DataStoreService")
local Storage = Data:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "leaderstats".
local Currency = Instance.new("IntValue", Folder)
Currency = "Point" -- Name of the player's currency
end)
local function SaveDataASYNC(STRage, Key, Value)
local bool = false
local ignored, STR = pcall(function() bool = true end)
if bool then STRage:SetAsync(Key, Value) else
warn("\nErrot to save data player, Reply:\t" .. STR)
end -- Two arguments must be passed to the function and the first one is the path with which the data will be saved and the second one is what is going to be saved. (SetAsync)
end
game.Players.PlayerRemoving:Connect(function(Player)
local Folder = Player:FindFirstChild("leaderstats") -- Name folder player
if Folder then
local Point = Folder:FindFirstChild("Point") -- Name Point Player
if Point then
SaveDataASYNC(Storage, "MyKeyID" .. Player.UserId, Point.Value)
end
end
end)
With that we finish the “Main” save, so to speak, now let’s go for the second one which is BindToClose()
this function waits a total of 30 seconds for the function given in the first argument to finish executing and if in that time it is not finished executing, the game will close without caring if the BindToClose()
function is finished executing or not. We will do this as follows:
local Data = game:GetService("DataStoreService")
local Storage = Data:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "leaderstats".
local Currency = Instance.new("IntValue", Folder)
Currency = "Point" -- Name of the player's currency
end)
local function SaveDataASYNC(STRage, Key, Value)
local bool = false
local ignored, STR = pcall(function() bool = true end)
if bool then STRage:SetAsync(Key, Value) else
warn("\nErrot to save data player, Reply:\t" .. STR)
end -- Two arguments must be passed to the function and the first one is the path with which the data will be saved and the second one is what is going to be saved. (SetAsync)
end
game.Players.PlayerRemoving:Connect(function(Player)
local Folder = Player:FindFirstChild("leaderstats") -- Name folder player
if Folder then
local Point = Folder:FindFirstChild("Point") -- Name Point Player
if Point then
SaveDataASYNC(Storage, "MyKeyID" .. Player.UserId, Point.Value)
end
end
end)
-- Secondary storage
game:BindToClose(function()
local Players = game.Players:GetPlayers()
for _, Player in pairs(Players) do
coroutine.wrap(SaveDataASYNC)(Storage, "MyKeyID" .. Player.UserId, Point.Value)
end
end)
Una vez terminado con el guardado de datos, procedemos con la Obtención de datos con “GetAsync()” y importante usar el pcall
local Data = game:GetService("DataStoreService")
local Storage = Data:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "leaderstats".
local Currency = Instance.new("IntValue", Folder)
Currency = "Point" -- Name of the player's currency
coroutine.wrap(
pcall(function()
local rrepeat = 1
repeat wait()
rrepeat = rrepeat + 1
if Storage:GetAsync() == nil then Currency.Value = math.random(1, 2); break end
Currency.Value = Storage:GetAsync()
until Currency.Value >= 1 or rrepeat > 5
print("\nData loading is complete!")
end))
end)
And with that we would come already obtaining the data and saving them, I hope and my answer is useful to you. If I explained myself wrong or for lack of information, please let me know.
This is how it should look like once finished:
local Data = game:GetService("DataStoreService")
local Storage = Data:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "leaderstats".
local Currency = Instance.new("IntValue", Folder)
Currency = "Point" -- Name of the player's currency
coroutine.wrap(
pcall(function()
local rrepeat = 1
repeat wait()
rrepeat = rrepeat + 1
if Storage:GetAsync() == nil then Currency.Value = math.random(1, 2); break end
Currency.Value = Storage:GetAsync()
until Currency.Value >= 1 or rrepeat > 5
print("\nData loading is complete!")
end))
end)
local function SaveDataASYNC(STRage, Key, Value)
local bool = false
local ignored, STR = pcall(function() bool = true end)
if bool then STRage:SetAsync(Key, Value) else
warn("\nErrot to save data player, Reply:\t" .. STR)
end -- Two arguments must be passed to the function and the first one is the path with which the data will be saved and the second one is what is going to be saved. (SetAsync)
end
game.Players.PlayerRemoving:Connect(function(Player)
local Folder = Player:FindFirstChild("leaderstats") -- Name folder player
if Folder then
local Point = Folder:FindFirstChild("Point") -- Name Point Player
if Point then
SaveDataASYNC(Storage, "MyKeyID" .. Player.UserId, Point.Value)
end
end
end)
-- Secondary storage
game:BindToClose(function()
local Players = game.Players:GetPlayers()
for _, Player in pairs(Players) do
coroutine.wrap(SaveDataASYNC)(Storage, "MyKeyID" .. Player.UserId, Point.Value)
end
end)
This is to save player data, if you want player position you have to change lines
These just looks like some generic DataStore scripts you’ve decided to copy and paste into the thread. They aren’t even relevant to the thread’s topic (storing/loading a player’s character’s position when they leave/join the game respectively).
Anyway, I decided to write a script for this and it worked well when testing.
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterRemoving:Connect(function(character)
local otherPlayer = players.PlayerRemoving:Wait()
if player == otherPlayer then
local data = {character:GetPivot():GetComponents()}
local success, result = pcall(function()
return datastore:SetAsync(player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
end)
local character = player.Character or player.CharacterAdded:Wait()
local success, result = pcall(function()
return datastore:GetAsync(player.UserId)
end)
if success then
if result then
if type(result) == "table" then
print(result)
character:PivotTo(CFrame.new(table.unpack(result)))
end
end
else
warn(result)
end
end)
game:BindToClose(function()
for _, player in ipairs(players:GetPlayers()) do
local character = player.Character
if character then
local data = {character:GetPivot():GetComponents()}
local success, result = pcall(function()
return datastore:SetAsync(player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
end
end)
Ohhhh sorry, I was trying to report the DataStore, and then say that for it to save the player’s position I would have to move some lines .