Why is my remote function returning nil in a callback?

Hey

I have a script that when you click a proximity prompt, It makes a GUI show the name of the prompt. But for some reason, When I test and use it, It just sayings “Nil”
image
This is my script

Server

script.Parent.Triggered:Connect(function(player)
	if player then
		local name = script.Parent.Parent.Parent.Name
		game.ReplicatedStorage.Events.Painting:InvokeClient(player, name)
		return true
	else
		return false
	end
end)

Client

game.ReplicatedStorage.Events.Painting.OnClientInvoke = function(player, name)
	local new_notification = script.Parent.Notifications.Notification_Painting:Clone()
	new_notification.Visible = true
	new_notification.Parent = script.Parent.Notifications
script.Parent.Notifications.Notification_Painting.Desc.Text = "This painting it titled '"..tostring(name).."'!"
	script.Pop:Play()
	wait(2)
	TweenService:Create(new_notification,TweenInfo.new(1),{BackgroundTransparency = 1}):Play()
	TweenService:Create(new_notification.Desc,TweenInfo.new(1),{TextTransparency = 1}):Play()
	wait(1)
	new_notification:Destroy()
	return true
end
1 Like

your only passing one argument:
game.ReplicatedStorage.Events.Painting:InvokeClient(player, name)
it invokes player’s client and sends name

but in the client script:
game.ReplicatedStorage.Events.Painting.OnClientInvoke = function(player, name)
you took two arguments: (player, name)

i think you meant to type:
game.ReplicatedStorage.Events.Painting.OnClientInvoke = function(name)
?

1 Like

When you invoke a RemoteFunction on the server, the player is not sent, also your “player” argument becomes “name” and so “name” is equal to nil.

If you want to retrieve both pieces of information, replace “player” with “name” and retrieve the player using the “Players” service.

local Players = game:getService("Players")
local LocalPlayer = Players.LocalPlayer

And

game.ReplicatedStorage.Events.Painting.OnClientInvoke = function(name) as suggested by Winbloo

1 Like

To expand on what has been said by @Winbloo above.

When you use :InvokeClient() two arguments exist:

  • Player - The player you wish to send the data too.
  • …: data - Which can be any amount of data.

The first argument is only used by the server to know who to send the data too, the client already knows who they are (game.Players.LocalPlayer) and so this is unnecessary information. As such the argument is not passed over.

I would suggest reading the documentation provided by Roblox for more information about how things work as its all outline here (its also a life skill for coding to be honest):

As shown under the “arguments” section, it says that it only sends the parameters sent through.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.