Why Isn't My RemoteFunction Firing?

Hello there. I am working on a project right now, and part of it has to fire RemoteFunctions from Player > Server > Player2

It is not working apparently. On the server I made it print “sent” after it fires the event, and it does. On the client side of player2, I also made it print something when it receives the event, but it isn’t getting it!! I tried sending it to other players, and even to myself! There were no errors in the console. I have provided screenshots/code down below. Please help!!

Code:

Client:

local ids = {}

function create(text, typeOf, image, trading, player2)
	local new = script.Parent.TemplateItem:Clone()
	if not trading then
		new:WaitForChild("Top"):WaitForChild("Notification").Visible = true
		new.Top:WaitForChild("Trading").Visible = false
		new.Top.Notification:WaitForChild("DescriptionVal").Value = text
		if typeOf == "image" then
			new.Top.Notification:WaitForChild("Object"):WaitForChild("Image").Image = image
		elseif typeOf == "object" then
			local object = image:Clone()
			object.Parent = new.Top.Notification:WaitForChild("Object")
		end
		new.Name = text
	else
		new:WaitForChild("Top"):WaitForChild("Notification").Visible = false
		new.Top:WaitForChild("Trading").Visible = true
		new.Top.Trading:WaitForChild("TargetedPlayer").Value = player2
		local image, isReady = game:GetService("Players"):GetUserThumbnailAsync(player2.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		new.Top.Trading:WaitForChild("Image").Image = image
		new.Name = player2.UserId
	end
	new.Visible = true
	new.Parent = script.Parent
	new:TweenSize(UDim2.new(1, 0, 0.47, 0), "Out", "Sine", 0.2, true)
	local id = #ids + 1
	table.insert(ids, {Text = text, Object = new})
	if not trading then
		wait(7)
		new:TweenSize(UDim2.new(0, 0, 0, 0), "In", "Sine", 0.2, true)
		wait(0.2)
		new:Destroy()
		table.remove(ids, id)
	end
end


game.ReplicatedStorage.Items.Events.RemoteEvents.Trade.OnClientInvoke = function(action, data)
	print("got")
	if action == "SendTrade" then
		print("send trad")
		local player2 = data.Player2
		local player2Trading = player2:WaitForChild("Trading")
		local player2TradesAllowed = player2Trading:WaitForChild("TradesAllowed")
		local player2TradingWith = player2Trading:WaitForChild("TradingWith")
		if player2TradesAllowed.Value and player2TradingWith.Value == "" then
			print("can trade")
			if not script.Parent:FindFirstChild(player2.UserId) then
				print("doesnt exist")
				if #ids >= 2 then
					print("deleting old")
					local toTween = ids[1].Object
					toTween:TweenSize(UDim2.new(0, 0, 0, 0), "In", "Sine", 0.2, true)
					wait(0.2)
					toTween:Destroy()
					create(player2.UserId, "", "", true, player2)
					print("created")
				else
					create(player2.UserId, "", "", true, player2)
					print("created")
				end
			end
		end
	end
end

game.ReplicatedStorage.Items.Events.RemoteEvents.Notification.OnClientEvent:Connect(function(text, typeOf, image)
	if script.Parent:FindFirstChild(text) then
		script.Parent[text].Notification.Top:WaitForChild("Count").Value += 1
	else
		if #ids >= 2 then
			local toTween = ids[1].Object
			toTween:TweenSize(UDim2.new(0, 0, 0, 0), "In", "Sine", 0.2, true)
			wait(0.2)
			toTween:Destroy()
			create(text, typeOf, image)
		else
			create(text, typeOf, image)
		end
	end
end)

print("connected to all functions")

Server:

local replicatedStorage = game:GetService("ReplicatedStorage")
local items = replicatedStorage:WaitForChild("Items")
local remoteEvents = items:WaitForChild("Events"):WaitForChild("RemoteEvents")
local tradeEvent = remoteEvents:WaitForChild("Trade")

tradeEvent.OnServerInvoke = function(player, action, data)
	local returning
	if action == "SendTrade" then
		local playerTrading = player:WaitForChild("Trading")
		local playerTradesAllowed = playerTrading:WaitForChild("TradesAllowed")
		local playerTradingWith = playerTrading:WaitForChild("TradingWith")
		local player2 = data.Player2
		local player2Trading = player2:WaitForChild("Trading")
		local player2TradesAllowed = player2Trading:WaitForChild("TradesAllowed")
		local player2TradingWith = player2Trading:WaitForChild("TradingWith")
		if (player2TradesAllowed.Value and player2TradingWith.Value == "") and (playerTradesAllowed.Value and playerTradingWith.Value == "") then
			returning = true
			tradeEvent:InvokeClient(player2, "SendTrade", {Player2 = player})
			print("successfully sent")
		end
	elseif action == "AcceptTrade" then
		local playerTrading = player:WaitForChild("Trading")
		local playerTradesAllowed = playerTrading:WaitForChild("TradesAllowed")
		local playerTradingWith = playerTrading:WaitForChild("TradingWith")
		local player2 = data.Player2
		local player2Trading = player2:WaitForChild("Trading")
		local player2TradesAllowed = player2Trading:WaitForChild("TradesAllowed")
		local player2TradingWith = player2Trading:WaitForChild("TradingWith")
		if (player2TradesAllowed.Value and player2TradingWith.Value == "") and (playerTradesAllowed.Value and playerTradingWith.Value == "") then
			returning = true
			playerTradingWith.Value = player2.UserId
			player2TradingWith.Value = player.UserId
			tradeEvent:InvokeClient(player, "StartTrade", {Player2 = player2})
			tradeEvent:InvokeClient(player2, "StartTrade", {Player2 = player})
		end
	end
	return returning
end


1 Like

I never really used remote functions before so I might not be much of help. Are you sure there isn’t anything preventing the client script from running. Also, use the format so we can read your code easier.

  1. No there is nothing preventing the client script from running, I made it print after all the connections and functions “connections complete”

  2. I will edit the post to make the code easier to read.

Also, make sure you are invoking the correct event. On the second image, I see the remote function is called Trade not tradeEvent.

Yes, I know that. In the client script, “tradeEvent” isn’t defined, I just put in the path to the event. On the server, I did define it, and it has the same path.

where is the local script located?

In StarterGui, under the main GUI.

I can’t really see a reason it won’t work. lets just try reading through the docs to see if you did anything wrong.

1 Like

It seems as this is game-breaking so I think you should stick to using remote events. There isn’t really a difference between what you use anyway.

I decided to use the other event on the client script, and it successfully worked :relieved:

Thanks.

1 Like

Your welcome :slightly_smiling_face:

Ok, lemme explain some things.

What are Remote Functions?

Remote Functions is a way to request information from Client to Server or Server to Client AND returning Data. This means Remote Functions are a 2 way Communication

*When to use RemoteFunctions?

Remote Functions should be used when Fetching Data. For example if I have a code system and wanted to ask Server if this code is valid then server can tell you. Based on that information you can tell whether it is valid code or not. Or even if it’s already redeemed. This can be used for Fetching a table too. If you wanted to make a Pet System it is recommended to Fetch a table from server and then create all pets when you join by literating.

Do/Do Not

DO NOT INVOKE CLIENT. THIS IS BAD PRACTICE. A hacker can manipulate something and cause MANY problems

ALSO

DO NOT MISUSE EACH THING. IF YOU DO NOT NEED 2 WAY COMMUNICATION THEN USE REMOTE EVENTS.

:slight_smile:

1 Like

Thanks for the tip. I am wayyy past that now. Thanks though!