I’m relatively new to data stores and I am trying to save my a string value to leaderstats
I am not sure why my code isn’t working(Int Values save fine), here is my code
local data = DataStore:GetDataStore("SaveAlias")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local save = Instance.new("StringValue",leader)
save.Name = "Alias"
save.Value = data:GetAsync(player.UserId) or "daffie"
data:SetAsync(player.UserId, save.Value)
save.Changed:connect(function()
data:SetAsync(player.UserId, save.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
data:SetAsync(player.UserId, player.leaderstats.Alias.Value)
end)```
I dont know why it doesnt save my data-
didn’t work, pretty much did the same thing my script did
game.Players.PlayerAdded:connect(function(player)
local key = "key-”…player.userId"
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local cash = Instance.new("StringValue",folder)
cash.Name = "Alias"
local save = DataStore:GetAsync(key)
if save then
cash.Value = save[1]
else
local cashload = {cash.Value}
DataStore:SetAsync(key,cashload)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local key = "key-"..player.userId
local load = {player.leaderstats:FindFirstChild("Cash").Value}
DataStore:SetAsync(key,load)
end)```
You absolutely can save strings. The issue here is more likely because the server shuts down before it can finish saving. You should use a BindToClose function to ensure the server has enough time to save data before closing when the last player leaves.
I added in a bind to close with your script, and everything saved fine. Granted, it did cause a request full since I changed it and immediately left, but it was still saved nonetheless. All you need to do is add
game:BindToClose(function()
wait(3)
end)
Also, I believe the underlying issue if that doesnt work is the way you are changing the value. You are most likely changing it locally, so the server is saving only what it sees.
There are many methods of saving data on roblox, I prefer datastores because the others are too hard… But yeah, you need a bindtoclose. Also you need your “Enable Studio Access to API Services” on.