I want to work on something related to DataStores and Values, but I don’t know how to [not] save the data while in studio, any help?
Script:
local DataStoreService = game:GetService("DataStoreService")
local plrData = DataStoreService:GetDataStore("PlrData")
game:GetService("Players").PlayerAdded:Connect(function(player)
--Stats
local StatsFolder = Instance.new("Folder")
StatsFolder.Parent = player
StatsFolder.Name = "Stats"
local Hunger = Instance.new("IntValue") -- Saturation
Hunger.Parent = StatsFolder
Hunger.Name = "Hunger"
local Thirst = Instance.new("IntValue") -- Hydration
Thirst.Parent = StatsFolder
Thirst.Name = "Thirst"
-- DataStore
local plrUserId = "Player_" .. player.UserId
local data = plrData:GetAsync(plrUserId)
if data then
Hunger.Value = data['Hunger']
Thirst.Value = data['Thirst']
else
Hunger.Value = 100
Thirst.Value = 100
end
end)
local function createTable(player)
local plrStats = {}
for _, stat in pairs(player.Stats:GetChildren()) do
plrStats[stat.Name] = stat.Value
end
return plrStats
end
local function onPlayerExit(player)
local plrStats = createTable(player)
local success, err = pcall(function()
local plrUserId = "Player_" .. player.UserId
plrData:SetAsync(plrUserId, plrStats)
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerRemoving:Connect(onPlayerExit)