Add to a players data table with profile service?

I am making a tycoon and using Profile Service to save information. How would I manage to get that data of “ItemsOwned” and add to it and clear it?

Script I am using: (Not mine, i just edited it for my needs.)

-- ProfileTemplate table is what empty profiles will default to.
-- Updating the template will not include missing template values
--   in existing player profiles!
local ProfileTemplate = {
	Cash = 0,
	ItemsOwned = {},
	LogInTimes = 0,
}

----- Loaded Modules -----

local ProfileService = require(game.ServerScriptService.ProfileService)

----- Private Variables -----

local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerData",
	ProfileTemplate
)

local Profiles = {} -- [player] = profile

----- Private Functions -----

local function GiveVal(profile, amount, nameval)
	-- If "Cash" was not defined in the ProfileTemplate at game launch,
	--   you will have to perform the following:
	if profile.Data[nameval] == nil then
		profile.Data[nameval] = 0
	end
	-- Increment the "Cash" value:
	profile.Data[nameval] = profile.Data[nameval] + amount
end

local cc : BindableFunction = game.ReplicatedStorage.ReturnValServer
local gc : BindableEvent = game.ServerScriptService.gAdd

gc.Event:Connect(function(plr, ammount, val)
	local profile = Profiles[plr]
	GiveVal(profile, ammount, val)
end)

cc.OnInvoke = function(plr,val)
	local profile = Profiles[plr]
	if profile then
		return profile.Data[val]
	end
end

game.ReplicatedStorage.ReturnVal.OnServerInvoke = function(plr, val)
	local profile = Profiles[plr]
	if profile then
		return profile.Data[val]
	end
end

local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile ~= nil then
		profile:AddUserId(player.UserId) -- GDPR compliance
		profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
		profile:ListenToRelease(function()
			Profiles[player] = nil
			-- The profile could've been loaded on another Roblox server:
			player:Kick()
		end)
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
			-- A profile has been successfully loaded:
		else
			-- Player left before the profile loaded:
			profile:Release()
		end
	else
		-- The profile couldn't be loaded possibly due to other
		--   Roblox servers trying to load this profile at the same time:
		player:Kick() 
	end
end

----- Initialize -----

-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end

----- Connections -----

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = Profiles[player]
	if profile ~= nil then
		profile:Release()
	end
end)


----- Tycoon Data -----

local function check(tycoon, plr)
	local profile = Profiles[plr]
	if profile then
		return profile.Data["ItemsOwned"]
	end
end

while wait(1) do
	local tycoons = game.Workspace.Tycoons
	for i,v in pairs(tycoons:GetChildren()) do
		if v:FindFirstChild("Configuration") and v:FindFirstChild("Configuration").CurrentOwner.Value ~= nil then
			local plr = v:FindFirstChild("Configuration").CurrentOwner.Value
			check(v, plr)
		end
	end
end

Fixxed it myself fdshngnfdouignfisdjg

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