I’m trying to create a script where the game saves a string value called “Stand” but it doesn’t work. I’ve tried saving leader stats and had no problem doing that, but for some reason when I try to save a string it doesn’t work.
This is my code:
local datastore = game:GetService("DataStoreService"):GetDataStore("JoJo1")
function save(player)
if player:FindFirstChild("Data") then
local dataToSave = {["Stand"] = "None"}
for i, v in pairs(player:FindFirstChild("Data"):GetDescendants()) do
dataToSave[v.Name] = v.Value
end
datastore:SetAsync(player.UserId, dataToSave)
end
end
local function PlayerJoined(plr)
if plr:FindFirstChild("Data") then return end -- Player was already passed
local dataFolder = Instance.new("Folder", plr)
dataFolder.Name = "Data"
local stand = Instance.new("StringValue", dataFolder)
stand.Name = "Stand"
stand.Value = "None"
-- Make sure we actually load our data
local Status, Data = pcall(datastore.GetAsync, datastore, plr.UserId)
if Status then
if not Data then Data = {["Stand"] = "None"} end
else
task.defer(function()
Status, Data = pcall(datastore.GetAsync, datastore, plr.UserId)
if not Status then
warn(("Failed to load data for UserId %s\nProviding Temp data..."):format(plr.UserId))
Data = {["Stand"] = "None"}
end
end)
end
-- Now we can use the data.
if Data then
for key, value in pairs(Data) do
if dataFolder:FindFirstChild(key) then
dataFolder[key].Value = value
end
end
end
end
game.Players.PlayerAdded:Connect(PlayerJoined)
-- Sometimes players get in before PlayerAdded is connected.
for _, Player in pairs(game.Players:GetPlayers()) do
PlayerJoined(Player)
end
game:BindToClose(function()
for i, v in pairs(game.Players:GetDescendants())do
save(v)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if not game:GetService("RunService"):IsStudio() then
save(plr)
end
end)
Thank you for reading, any help is appreciated.