Hello there! I am trying to make a DataStore Script but it won’t save. At first, I thought the problem might be with GetAsync()
but I’ve printed it and it is nil. So the problem is with :SetAsync()
.
Script:
---------------------------------------------- DATASTORE ----------------------------------------------
local Settings = require(script.Settings)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore(Settings["DataStoreName"])
game.Players.PlayerAdded:Connect(function(player)
local ReceivedData
local Folder = Instance.new("Folder",player)
Folder.Name = "leaderstats"
local DataValue = Instance.new("IntValue",Folder)
DataValue.Name = Settings["DataStoreCurrency"]
DataValue.Value = Settings["DefaultMoney"]
local Success, Error = pcall(function()
local ReceivedData = DataStore:GetAsync(player.UserId)
end)
if Success then
if ReceivedData ~= nil then
DataValue.Value = ReceivedData
end
else
if Settings["KickPlayerIfDataStoreFailedToLoad"] then
player:Kick(Settings["DataStoreFailedToLoadMessage"])
else
warn("DataStore failed to load for "..player.Name..". Error Message: "..Error)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ValueToSave = player.leaderstats[Settings["DataStoreCurrency"]].Value
local Success, Error = pcall(function()
DataStore:SetAsync(player.UserId,ValueToSave)
end)
if Success ~= true then
warn("DataStore failed to save for "..player.Name..". | DataStore: "..Settings["DataStoreName"].." | Currency: "..Settings["DataStoreCurrency"].." | Value to Save: "..ValueToSave)
end
end)
ModuleScript:
local Settings = {
["DataStoreName"] = "ChangeThis",
["DataStoreCurrency"] = "Cash",
["DefaultMoney"] = 50,
["DataStoreFailedToLoadMessage"] = "DataStore failed to load!",
["KickPlayerIfDataStoreFailedToLoad"] = false
}
return Settings