How would I make my script that gives a certain player that is in a different server an amount of cash work using MessagingService?

Hello, I’m wondering why my script does not work. It basically gives cash to another player that is in a different server upon purchasing that players item in his store using Messaging Service. I get an error saying “attempt to index nil with Name”.
Server script 1:

local function test(player, targetPlayerName, amount)

	local success, err = pcall(function()
		MessagingService:PublishAsync("CashTransfer", {
			senderName = player.Name,
			targetName = targetPlayerName,
			amount = amount
		})
	end)

	if not success then
		warn("Failed to send message to Server B: " .. err)
	end
end


local playerBName = "RetaIiation_Z" 
local amountToTransfer = 50 




itemSoldEvent.OnServerEvent:Connect(function(player, price, ownerName, nameLimited)
	test(player, playerBName, amountToTransfer)
end)

server script 2:

local MessagingService = game:GetService("MessagingService")
local function onCashTransferReceived(messageData)
	if messageData and typeof(messageData) == "table" then
		local senderName = messageData.Data.senderName
		local targetName = messageData.Data.targetName
		print(senderName,targetName)
		local amount = messageData.amount
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.Name == targetName then
				local targetPlayer = v
				print("Found")
				break
			end
		end
		local targetPlayer = game.Players:FindFirstChild(targetName)
	
		print(targetPlayer.Name)
		if targetPlayer then
			print("Found him!")
			local leaderstats = targetPlayer:WaitForChild("leaderstats")
			if leaderstats then
				print("leaderstats found")
			end
			if leaderstats and leaderstats:FindFirstChild("Cash") then
				-- Update Player B's Cash
				local targetCash = leaderstats.Cash
				targetCash.Value = targetCash.Value + amount

				-- Optionally, send a message to Player B to notify them
				print(targetName .. " received " .. amount .. " cash from " .. senderName)
			else
				warn("Player B does not have a Cash leaderboard stat.")
			end
		else
			warn("Target player not found in the server: " .. targetName)
		end
	end
end

local success, err = pcall(function()
	MessagingService:SubscribeAsync("CashTransfer", onCashTransferReceived)
end)

if not success then
	warn("Failed to subscribe to CashTransfer: " .. err)
end

you’re getting a "attempt to index nil with Name" error because you’re trying to run print(targetPlayer.Name) even if you didn’t find the player with game.Players:FindFirstChild(targetName).

this means that it’s not finding the player with the targetName, so if you’re sure you didn’t mess up the playerBName variable and are on that account in the other server, then you should provide your console when these prints occur like the senderName and targetName variables, it could help in debugging.

and by the way, MessagingService relays that message to every game server instance of the place, not just ServerB.