Remote Function Not Working

This script is supposed to fire a remote function. Line 15: print(selectedItem2.Value) does work, but the line after it which fires the remote function does not seem to work. I added some prints to the server script and nothing was printed. (let me know if you want to see anything else) Thanks

local player = game.Players.LocalPlayer
local playerName = player.Name
local replicatedStorage = game:GetService("ReplicatedStorage")

local selectedItem2 = script.Parent.Parent.Parent:WaitForChild("SelectedItem2")

local success--Variable

script.Parent.MouseButton1Click:Connect(function()
	
	success = false--Makes success false
	if selectedItem2.Value ~= nil then--If the value isn't equal to nil
		selectedItem2.Value = script.Parent.Parent.Name
		
		print(selectedItem2.Value)
		success = replicatedStorage.Equip:InvokeServer(selectedItem2.Value)--Make a request to the remote function
		
	end
	
	if success then
		print("Equiped!")
		
	else
			
			print("Equip Failed!")
	end
	
end)

ServerScript

game.ReplicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function  request
local ServerStorage = game:GetService("ServerStorage")
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage

	print("Yo")
	if not player.Backpack:FindFirstChild("LongStick") then--Check to see if the player has enough money
		print("Ello")
		local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
		gear.Parent = player.Backpack--Put the clone in their backpack
		print("Ye")
		
		return true
	else
			return false
	
	end
end)
2 Likes

How are you doing it on the server? A common mistake is that people do

remote.OnServerInvoke:Connect(function(args))

but that will not work as you do not connect remote functions, you bind them to functions.

It should be

remote.OnServerInvoke = function(args))

If you did that and it is still not printing anything after the call, let me know.

6 Likes

You might wanna show the server script. It would help others solve your issue.

2 Likes

I love to optimize scripts to the best efficiency. I would suggest you use GetService for the player just in case it errors which it would not, but still counts.

Use:

remote.OnServerInvoke = fucntion(args))

And here is your whole script optimized.

local Players= game:GetService("Players")

local Player = Players.LocalPlayer

local playerName = player.Name

local replicatedStorage = game:GetService("ReplicatedStorage")

local selectedItem2 = script.Parent.Parent.Parent:WaitForChild("SelectedItem2")

local success

script.Parent.MouseButton1Click:Connect(function()
	
	success = false  --Makes success false
	if selectedItem2.Value ~= nil then--If the value isn't equal to nil
		selectedItem2.Value = script.Parent.Parent.Name
		
		print(selectedItem2.Value)
		success = replicatedStorage.Equip:InvokeServer(selectedItem2.Value)

end

if success then
		print("Equiped!")
	else
		print("Equip Failed!")
	end
	
end)

Can’t optimize much due to being optimized already except for using a variable for Player’s service. Also, why are you setting the variable to false? Variable’s not defined are nil, meaning nothing.

Please show your server script which is the event listener, there is nothing wrong with your local one.

2 Likes

Yeah, I did it like this and it’s not working

game.ReplicatedStorage.CheckSale.OnServerInvoke = (function(player,item)

1 Like

Can you show more of your code on the server, please?

1 Like

I was kinda following an AlvinBlox tutorial and that’s what he did. I also added my server script to the post.

2 Likes

That’s all of the code

Char

1 Like

It seems that one or both of the two variables at the start of the remote probably are yielding for smth that does not exist (waitforchild) and thus you see nothing printed. This would be the likely cause if you see no errors in the console.

2 Likes

I added print("9") to the second line right after the server is invoked and it doesn’t print. I think something is happening before the variables, but not sure what.

game.ReplicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function  request
	print("9")
local ServerStorage = game:GetService("ServerStorage")
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage

	print("Yo")
	if not player.Backpack:FindFirstChild("LongStick") then--Check to see if the player has enough money
		print("Ello")
		local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
		gear.Parent = player.Backpack--Put the clone in their backpack
		print("Ye")
		
		return true
	else
			return false
	
	end
end)
1 Like

No need to define the variable to false unless you are using a condition for it.

2 Likes

Your variables are messed up and maybe that’s the cause and that ) before function.

game.ReplicatedStorage.CheckSale.OnServerInvoke = function(player,item)--Responds to the remote function  request
	print("9")
local ServerStorage = game:GetService("ServerStorage")
local shopItems = ServerStorage:WaitForChild("ShopItems")--Defines the items in ServerStorage

	print("Yo")
	if not player.Backpack:FindFirstChild("LongStick") then--Check to see if the player has enough money
		print("Ello")
		local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
		gear.Parent = player.Backpack--Put the clone in their backpack
		print("Ye")
		
		return true
	else
			return false
	
	end
end)

You using :GetService 2 times, I fixed it and run the script again.

2 Likes