How do I disable DataSaving while In-Studio?

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)

I think you can check if game.RunService:IsStudio()
That boolean will be true, when you are playing the game in Studio.

1 Like

Thanks, I was just looking at a post and didn’t know this existed!

No problem! Here’s the link to the RunService:IsStudio() function if you want to learn more about it! :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.