ProfileService Error

Hey there, im MagicalTeardrop! This is actually my first post, so truly sorry if its incorrect, or I mess something up.

I’m a New(er) developer, i have quite a bit of experience though a lot of back-end things are still really confusing to me. So right now, I’m currently making a game were you hit this rock, and you get a fruit out of it. (subjected to change). Im using ProfileService, to make an inventory, then i used “PlayerDataHandler:AddToInv(plyr, “Grape”)” to add a grape to my inventory. the issue is i keep getting the error:

ServerScriptService.PlayerDataHandler:83: Profile Dose Not exist for (My User Id).

Ive tried many things. ive looked all over the developer form, and tried to find some tutorials, and i cant figure this out!? the currect scripts im using are:

--------------------------------------------
--//VARIABLES - SETTINGS//--

local rock = script.Parent -- Identifies 
local label = rock.Health
local HL = label.HealthLeft -- HL = health left )shows how much helth is left on the rock
local HealthBar = label.HealthBar -- Identifies the health bar
local CloneFLdr = label["Clone Folder"]
local ClickDetector = rock.ClickDetector

local TH = 100 -- TH = Total health (controlls how much health rocks have)
local SwingsLeft = TH
HL.Text = SwingsLeft

local HS = label.HealthSubtraction
local NewSubNotif = math.random(0, 6) / 100

local PunchDamage = 10

local SSS = game:GetService("ServerScriptService")
local PlayerDataHandler = require(SSS:WaitForChild("PlayerDataHandler"))

-------------------------------------
--//TWEENING SETTINGS//--

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(
	
	0.4,
	Enum.EasingStyle.Back,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

--------------------------------------------
--//FUNCTIONS - CONNECTIONS//--

rock.ClickDetector.MouseClick:Connect(function(plyr) --Creates function, and checks if player has hit the rock

	
	HealthBar.BackgroundColor3 = Color3.new(1, 1, 1):Lerp(Color3.new(0.258824, 1, 0.203922), SwingsLeft/TH)
	
	rock.Size = rock.Size - Vector3.new(0.25, 0.25, 0.25)
	wait(0.1)
	rock.Size = rock.Size + Vector3.new(0.25, 0.25, 0.25)
	
	--------------------------------------------
	--//TWEEN - HEALTH BAR//--
	
	TH = 100
	SwingsLeft = math.clamp(SwingsLeft - PunchDamage, 0, TH)

	TS:Create(HealthBar, TI, {Size = UDim2.new(SwingsLeft/TH, 0, 1, 0)}):Play()
	HL.Text = SwingsLeft
	
	if SwingsLeft/TH == 0 then
		task.wait(0.4)
		rock:Destroy()
		PlayerDataHandler:AddToInv(plyr, "Grape")
	end

	--------------------------------------------
	--//TWEEN - HEALTH DAMAGE//--

	NewSubNotif = math.random(0, 60)/100
	HS.Position = UDim2.new(NewSubNotif,0,0,0)
	HS.TextTransparency = 0
	local Hit = HS:Clone()
	task.wait()

	Hit.Parent = CloneFLdr
	Hit.Text = "- ".. PunchDamage
	TS:Create(Hit, TI, {Position = UDim2.new(NewSubNotif,0,-0.95,0)}):Play()
	task.wait(0.4)
	TS:Create(Hit, TweenInfo.new(0.25), {TextTransparency = 1}):Play() 
	wait(1)
	Hit:Destroy()
	
end)

^ This script is mainly about my rock, and isnt as relevant, but its were im trying to add my fruit.

--------------------------------------------
--//MODULES - TABLES//--

local PlayerDataHandler = {}

--------------------------------------------
--//FUNCTIONS//--

-- Sets Default to nothing 
local DataTemplate = {
	 Money = 0,
	 Inventory = {}
}

local ProfileService = require(game.ServerScriptService.ProfileService)
local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerProfile",
	DataTemplate
)

local Profiles = {}
local function PlayerAdded(Plyr)
	local Profile = ProfileStore:LoadProfileAsync("Player_"..Plyr.UserId)

	if Profile then
		Profile:AddUserId(Plyr.UserId)
		Profile:Reconcile()

		Profile:ListenToRelease(function()
			Profiles[Plyr] = nil
			Plyr:Kick()
		end)

		if not Plyr:IsDescendantOf(Players) then
			Profile:Release()
		else
			Profiles[Plyr] = Profile
		end

		if Plyr.Character then
			LoadBackpack(Plyr)
		end

		Plyr.CharacterAdded:Connect(function()
			LoadBackpack(Plyr)
		end)
	else
		Plyr:Kick()
	end
end

function LoadBackpack(plyr)
	local inv = PlayerDataHandler:Get(plyr, "Inventory")
	
	for _, item in ipairs(inv) do
		local Asset = game.ReplicatedStorage.Assets:FindFirstChild(item)
			if Asset then
				Asset:Clone().Parent = plyr.Backpack
				Asset:Clone().Parent = game.StarterPack.UnlockedFruits
		end
	end
end

 -- Checks IN  Data Handler
	function PlayerDataHandler:Init()
		for _, player in game.Players:GetPlayers() do
			task.spawn(PlayerAdded, player)
	end
	
	game.Players.PlayerAdded:Connect(PlayerAdded)
	game.Players.PlayerRemoving:Connect(function(Plyr)
		if Profiles[Plyr] then
			Profiles[Plyr]:Release()
		end
	end)
end


 -- Gets Profile
	local function getProfile(plyr)
		assert(Profiles[plyr], string.format("Profile Dose Not exist for %s", plyr.UserId))
		return Profiles[plyr]
end
	
--------------------------------------------
--//ADD INV - REMOVE INV//--
	
	--Add to inv function
function PlayerDataHandler:AddToInv(plyr, ItemName)
	self:Update(plyr, "Inventory", function(current)
		table.insert(current, ItemName)
		
		return current
	end)
	local Item = game.ReplicatedStorage.Assets:FindFirstChild(ItemName)
	if not Item then return end
	
		Item:Clone().Parent = plyr.Backpack
		Item:Clone().Parent = game.StarterPack.UnlockedFruits
end

	--Remove From Inv function
function PlayerDataHandler:RemoveToInv(plyr, ItemName)
	self:Update(plyr, "Inventory", function(current)
		if table.find(current, ItemName) then
			table.remove(current, table.find(current, ItemName))
		end
			return current
	end)
	
	if plyr.Backpack:FindFirstChild(ItemName) then
		plyr.Backpack[ItemName]:Destroy()
	elseif plyr.Character:FindFirstChild(ItemName) then
		plyr.Character[ItemName]:Destroy()
	
	end
end
--------------------------------------------
--//GET-SET-UPDATE//--
	
	--Get Function
function PlayerDataHandler:Get(Plyr, key)
	local profile = getProfile(Plyr)
	assert(profile.Data[key], string.format("Data Dose Not Exisit for the key: %s", key))
	
	return profile.Data[key]
end

	--Set Function
function PlayerDataHandler:Set(plyr, key, value)
	local profile = getProfile(plyr)
	assert(profile.Data[key], string.format("Data Dose Not Exisit for the key: %s", key))

	assert(type(profile.Data[key]) == type(value))
	
	profile.Data[key] = value
end

	--Update Function
function PlayerDataHandler:Update(plyr, key, CallBack)
	local profile = getProfile(plyr)
	
	local oldData = self:Get(plyr, key)
	local newData = CallBack(oldData)
	
	self:Set(plyr, key, newData)
end

--------------------------------------------
--//ENDING//--

return PlayerDataHandler

^ Then this is my module script.

in the rest of my outbox i have:

Stack Begin - Studio
Script ‘ServerScriptService.PlayerDataHandler’, Line 83 - function getProfile - Studio - PlayerDataHandler:83
Script ‘ServerScriptService.PlayerDataHandler’, Line 143 - function Update - Studio - PlayerDataHandler:143
Script ‘ServerScriptService.PlayerDataHandler’, Line 92 - function AddToInv - Studio - PlayerDataHandler:92
Script ‘Workspace.GRAPES(spawn).Rock.Rock Health’, Line 61 - Studio
Stack End

Any and all help is appreciated!!
Cheers!

  • MagicalTeardrop (:
1 Like

Try this i guess?

--//MODULES - TABLES//--

local PlayerDataHandler = {}

--------------------------------------------
--//FUNCTIONS//--

-- Sets Default to nothing 
local DataTemplate = {
	 Money = 0,
	 Inventory = {}
}

local ProfileService = require(game.ServerScriptService.ProfileService)
local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerProfile",
	DataTemplate
)

local Profiles = {}
local function PlayerAdded(Plyr)
	local Profile = ProfileStore:LoadProfileAsync("Player_"..Plyr.UserId)

	if Profile then
		Profile:AddUserId(Plyr.UserId)
		Profile:Reconcile()

		Profile:ListenToRelease(function()
			Profiles[Plyr] = nil
			Plyr:Kick()
		end)

		if not Plyr:IsDescendantOf(Players) then
			Profile:Release()
		else
			Profiles[Plyr] = Profile
		end

		if Plyr.Character then
			LoadBackpack(Plyr)
		end

		Plyr.CharacterAdded:Connect(function()
			LoadBackpack(Plyr)
		end)
	else
		Plyr:Kick()
	end
end

function LoadBackpack(plyr)
	local inv = PlayerDataHandler:Get(plyr, "Inventory")
	
	for _, item in ipairs(inv) do
		local Asset = game.ReplicatedStorage.Assets:FindFirstChild(item)
			if Asset then
				Asset:Clone().Parent = plyr.Backpack
				Asset:Clone().Parent = game.StarterPack.UnlockedFruits
		end
	end
end

 -- Checks IN  Data Handler
	function PlayerDataHandler:Init()
		for _, player in game.Players:GetPlayers() do
			task.spawn(PlayerAdded, player)
	end
	
	game.Players.PlayerAdded:Connect(PlayerAdded)
	game.Players.PlayerRemoving:Connect(function(Plyr)
		if Profiles[Plyr] then
			Profiles[Plyr]:Release()
		end
	end)
end


 -- Gets Profile
	local function getProfile(plyr)
		if Profiles[plyr] then
			return Profiles[plyr]
		else
			error(string.format("Profile Does Not exist for %s", plyr.UserId))
		end
end
	
--------------------------------------------
--//ADD INV - REMOVE INV//--
	
	--Add to inv function
function PlayerDataHandler:AddToInv(plyr, ItemName)
	if getProfile(plyr) then
		self:Update(plyr, "Inventory", function(current)
			table.insert(current, ItemName)
			
			return current
		end)
		local Item = game.ReplicatedStorage.Assets:FindFirstChild(ItemName)
		if not Item then return end
		
			Item:Clone().Parent = plyr.Backpack
			Item:Clone().Parent = game.StarterPack.UnlockedFruits
	end
end

	--Remove From Inv function
function PlayerDataHandler:RemoveToInv(plyr, ItemName)
	self:Update(plyr, "Inventory", function(current)
		if table.find(current, ItemName) then
			table.remove(current, table.find(current, ItemName))
		end
			return current
	end)
	
	if plyr.Backpack:FindFirstChild(ItemName) then
		plyr.Backpack[ItemName]:Destroy()
	elseif plyr.Character:FindFirstChild(ItemName) then
		plyr.Character[ItemName]:Destroy()
	
	end
end
--------------------------------------------
--//GET-SET-UPDATE//--
	
	--Get Function
function PlayerDataHandler:Get(Plyr, key)
	local profile = getProfile(Plyr)
	assert(profile.Data[key], string.format("Data Does Not Exist for the key: %s", key))
	
	return profile.Data[key]
end

	--Set Function
function PlayerDataHandler:Set(plyr, key, value)
	local profile = getProfile(plyr)
	assert(profile.Data[key], string.format("Data Does Not Exist for the key: %s", key))

	assert(type(profile.Data[key]) == type(value))
	
	profile.Data[key] = value
end

	--Update Function
function PlayerDataHandler:Update(plyr, key, CallBack)
	if getProfile(plyr) then
		local oldData = self:Get(plyr, key)
		local newData = CallBack(oldData)
		
		self:Set(plyr, key, newData)
	end
end

--------------------------------------------
--//ENDING//--

return PlayerDataHandler

I tried it and it unfortunately didn’t work… what i think the issue could be is when i call the “PlayerDataHandler:AddToInv(plyr, “Grape”)”, because if i were to do a different script and use it it works completely fine. : I did receive the same stuff in the output though.