Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does it say player is nil in my remoteevent?

Basically, this event fires from a server script when a player purchases a product. In my client script, I check if it's the right player by doing if game.Players.LocalPlayer.Name = plr.Name then... It says that the plr value is nil even tho I'm getting it from the OnClientEvent function.

-- Server Script
local MarketplaceService = game:GetService("MarketplaceService")
 local event = game.ReplicatedStorage["Music System"].PurchaseMusic
local function processReceipt(receiptInfo)
		local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
		if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
		end
		if receiptInfo.ProductId == 655383631 then
			event:FireClient(player)
		end
		return Enum.ProductPurchaseDecision.PurchaseGranted
end
 
MarketplaceService.ProcessReceipt = processReceipt

And my client script:

-- Client Script/Local Script
local event = game.ReplicatedStorage["Music System"].RequestMusic
local event2 = game.ReplicatedStorage["Music System"].PurchaseMusic

event2.OnClientEvent:Connect(function(plr)
	if game.Players.LocalPlayer.Name == plr.Name then
	local id = script.Parent.Parent.ChosenID.Value
	event:FireServer(id)
	script.Parent.Text = "Added To Queue"
	end
end)

6 comments

0

Try event2.OnClientEvent:Connect(function(Rand, plr) for the client script. I really don't know why roblox does this, but I encounter this all the time

0

what is rand?

0

A nil value. For some reason, when receiving parameters passed via a remote event, the values are offset by one

0

also that doesn't work

0

Players.TypicallyPacific.PlayerGui.ScreenGui.Frame.TextButton.LocalScript:5: attempt to index local 'plr' (a nil value)

0

Can you test to see if you are getting the player object?

1 answer

✓ Accepted answer

You have to fire from the server with an additional argument that includes the player. The first argument (the player) tells the game where to send the fire event to (the specified player) so technically it's not an argument when its received from the client's side.

Also, you can't send instances via remotes so you're gonna have to send the name instead.

(btw i removed the player because you don't need to check it, if its sent to the client it was on purpose by the server, it should work nevertheless)

Server

-- Server Script
local MarketplaceService = game:GetService("MarketplaceService")
local event = game.ReplicatedStorage["Music System"].PurchaseMusic
local function processReceipt(receiptInfo)
	local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	if receiptInfo.ProductId == 655383631 then
		event:FireClient(player)
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = processReceipt

Client

-- Client Script/Local Script
local event = game.ReplicatedStorage["Music System"].RequestMusic
local event2 = game.ReplicatedStorage["Music System"].PurchaseMusic

event2.OnClientEvent:Connect(function(plr)
	local id = script.Parent.Parent.ChosenID.Value
	event:FireServer(id)
	script.Parent.Text = "Added To Queue"
end)

2 comments

1

Alright, so it'll only go to the player who purchased it right?

0

Absolutely, the server automatically does this!

This discussion is preserved as it was. New answers, comments, and votes are closed.