Saving a string value to leaderstats through data store

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-

First turn on the API services for data store to work.

But I already have Api services enabled, my problem is string not saving

Well, first of all you have no functions. Let me share my cash saving script.

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“StoreName”)

game.Players.PlayerAdded:connect(function(player)
local key = “key-”…player.userId
local folder = Instance.new(“Folder”,player)
folder.Name = “leaderstats”
local cash = Instance.new(“IntValue”,folder)
cash.Name = “Cash”

 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)

Change the things according to your leaderboard and it should be good. Tell me if it does not work

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)```

Wait I just realize I messed up the script let me retry

Still didn’t work, I’m pretty sure you cant save strings to datastore, or is there another way?

You need to edit the script more. Still missing things.

Ok can you explain what am i missing?

Just clarifying you can understand your script right?

Yes I can understand my script, I’m new to data stores but I still know some things

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.

2 Likes

I tried that but it didn’t work

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.

I’ve already tried that I believe there is another method to save strings that I am not aware of

Can you show me the script that changes the value by inputting text into the textbox

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.

1 Like