Getting data from ProfileService not working correctly

Hello,

This is my first time using ProfileService. I was trying to create two read and get data functions that use RemoteEvents and RemoteFunctions to communicate between the server and client.
However, when I try to read and print from the data, it prints nil every time. Also, when I try to change the data and print that, it prints the name of the object in the dictionary instead of the value. However, this only happens to strings, and not integers.
Below are my scripts:

Server-side
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local players = game:GetService("Players")

local modules = replicatedStorage:WaitForChild("Modules")
local profileService = require(modules:WaitForChild("ProfileService"))

local events = replicatedStorage:WaitForChild("Events")
local changeDataEvent = events:WaitForChild("ChangeData")
local getDataFunction = events:WaitForChild("GetData")

local profileTemplate = {
	feathers = 0,
	exp = 0,
	pillow = "Default",
	knockouts = 0
}

local profileStore = profileService.GetProfileStore(
	"playerData",
	profileTemplate
)

local profiles = {}

local function getData(player, data)
	local profile = profiles[player]

	if profile ~= nil then
		return profile.Data[data]
	end
end

local function changeData(player, dataToChange, value)
	local profile = profiles[player]

	if profile ~= nil then
		if type(value) == "string" then
			profile.Data[dataToChange] = value
		elseif type(value) == "number" then
			profile.Data[dataToChange] = profile.Data[dataToChange] + value
		end
	end
end

local function playerAdded(player)
	local profile = profileStore:LoadProfileAsync("Player_" .. player.UserId)
	
	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		profile:ListenToRelease(function()
			profiles[player] = nil
			
			player:Kick("Unable to load your data. Please rejoin.")
		end)
		
		if player:IsDescendantOf(players) == true then
			profiles[player] = profile
			
			-- A profile is successfully loaded
		else
			profile:Release()
			
			-- Player left before profile loaded
		end
	else
		player:Kick("Unable to load your data. Please rejoin.")
		
		-- Profile couldn't be loaded, possibly due to other servers trying to load it at the same time
	end
end

local function playerRemoved(player)
	local profile = profiles[player]

	if profile ~= nil then
		profile:Release()
	end
end

for _, player in ipairs(players:GetPlayers()) do -- Runtime failsafe
	task.spawn()
end

players.PlayerAdded:Connect(playerAdded)

players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function() -- Server shutdown failsafe
	for _, player in ipairs(players:GetPlayers()) do
		playerRemoved(player)
	end
end)

changeDataEvent.OnServerEvent:Connect(changeData)
getDataFunction.OnServerInvoke = function()
	getData()
end```
Client-side (GUI)
local replicatedStorage = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")

local player = players.LocalPlayer

local modules = replicatedStorage:WaitForChild("Modules")

local events = replicatedStorage:WaitForChild("Events")

local changeDataEvent = events:WaitForChild("ChangeData")

local getDataFunction = events:WaitForChild("GetData")

script.Parent.Frame.Default.MouseButton1Click:Connect(function()

changeDataEvent:FireServer(player, "pillow", "Default")

end)

script.Parent.Frame.Cobblestone.MouseButton1Click:Connect(function()

changeDataEvent:FireServer(player, "pillow", "Cobblestone")

end)

while task.wait(0.1) do

local playerPillow = getDataFunction:InvokeServer(player, "knockouts")

print(playerPillow)

end

Thanks in advance! I appreciate any and all help I recieve.
Fizzitix

1 Like