How can I access int values that are instanced inside a script

I’m trying to learn how to save data in roblox, I copied a script and it works, but what I noticed is that it creates the int value in the script, does that mean that it cannot be changed by other scripts? or if it can how? and would I be able to create a int value without a script and still use it to save data?
this is the script

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("dataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("something went wrong")
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then 
		print("Player Data successfully saved!")
	else
		print("something went wrong")
		warn(err)
	end
end)
1 Like

The IntValue is being created in the script, but its parent is the leaderstats object the script also creates that is in the player.

It would look like this:

Player
→ Backpack
→ PlayerGui
→ …
→ leaderstats
→ -> Cash

Simply put, any script can access Cash as long as it has a reference to a Player object. To access it, you’d just reference Cash in the script you’re trying to access it from.