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