Remote Function will not :InvokeServer()

Carrying on from the help topic I just resolved regarding my weapon system, I’m now working on a system for one-time use tools. I’m going to stay light on the details (unless necessary otherwise) this time around because the problem is that, about 50% of the time during runtime, a specific remote function will straight up not :InvokeServer() when I tell it to. The function in question is called QueryInfo and is meant to gather information from server storage and return it to the client for proper tool and weapon handling. When I try to get that information, however, it will not communicate with the server; I know this because I’ve put print() statements in the .OnServerInvoke() function and they didn’t print anything in the output. This random nonfunctioning of QueryInfo has ground work on this system to a halt because I can’t test any of the tools unless I can get information from server storage. I’ve pasted the entire weapon system (from a game file where QueryInfo worked 100% of the time) into the game file with the one-time tool system and now even the scripts that utilize QueryInfo from the weapon system no longer work half the time.

Here are the implementations:

-- tool system local
character.ChildAdded:Connect(function(tool)
	if tool.ClassName ~= "Tool" then return end -- return if it's not a tool that's being added
	--zeroing out connections
	if equipConnection then equipConnection:Disconnect() equipConnection = nil end
	if unequipConnection then unequipConnection:Disconnect() unequipConnection = nil end
	-- grabbing the tool handle and attributes
	local handle = tool:WaitForChild("Handle")
	local throwable = tool:GetAttribute("Throwable") or nil
	local consumable = tool:GetAttribute("Consumable") or nil
	if not throwable and not consumable then return end
	-- function that runs when the tool is equipped
	equipConnection = tool.Equipped:Connect(function()
		--zeroing out connections
		if inputBegin then
			inputBegin:Disconnect()
			inputBegin = nil
		end
		
		RANGE, TIMER, DURATION, HEALTH = queryInfo:InvokeServer(tool)
		print(RANGE, TIMER, DURATION, HEALTH)

-- tool system server
function queryInfo.OnServerInvoke(player, tool)
	local itemConfig = getConfig(tool)
	print(itemConfig)
	if not itemConfig then return end
	
	local range, timer, duration, health
	
	if itemConfig:FindFirstChild("Range") then range = itemConfig.Range.Value end
	if itemConfig:FindFirstChild("Timer") then timer = itemConfig.Timer.Value end
	if itemConfig:FindFirstChild("Duration") then duration = itemConfig.Duration.Value end
	if itemConfig:FindFirstChild("Health") then health = itemConfig.Health.Value end
	
	return range, timer, duration, health
end

-- weapon system local
character.ChildAdded:Connect(function(tool)
	if tool.ClassName ~= "Tool" then return end -- return if it's not a tool that's being added
	--zeroing out connections
	if equipConnection then equipConnection:Disconnect() equipConnection = nil end
	if unequipConnection then unequipConnection:Disconnect() unequipConnection = nil end
	-- grabbing the tool handle and attributes
	local handle = tool:WaitForChild("Handle")
	local slashAnim = handle:FindFirstChild("Slash")
	local automatic = tool:GetAttribute("Automatic") or nil
	local melee = tool:GetAttribute("Melee") or nil
	if tool:GetAttribute("Throwable") or tool:GetAttribute("Consumable") then return end
	-- function that runs when the tool is equipped
	equipConnection = tool.Equipped:Connect(function()
		--zeroing out connections
		if inputBegin then inputBegin:Disconnect() inputBegin = nil end
		if inputEnd then inputEnd:Disconnect() inputEnd = nil end
		if firing then firing = nil end
		-- retrieve info, handle animations and GUI elements
		print("here")
		FIRE_RATE, FIRE_SPEED, FIRE_RANGE, FIRE_SPREAD, FIRE_BURST, FIRE_SHOT, mag, ammo = queryInfo:InvokeServer(tool)
		print(FIRE_RATE, FIRE_SPEED, FIRE_RANGE, FIRE_SPREAD, FIRE_BURST, FIRE_SHOT, mag, ammo)

-- weapon system server
function queryInfo.OnServerInvoke(player, tool)
	local weaponConfig = getConfig(tool)
	if not weaponConfig then return end

	local fireRate, fireSpeed, range, spread, burst, shot, mag, ammo
	
	if weaponConfig:FindFirstChild("FireRate") then fireRate = weaponConfig.FireRate.Value end
	if weaponConfig:FindFirstChild("FireSpeed") then fireSpeed = weaponConfig.FireSpeed.Value end
	if weaponConfig:FindFirstChild("Range") then range = weaponConfig.Range.Value end
	if weaponConfig:FindFirstChild("Spread") then spread = weaponConfig.Spread.Value end
	if weaponConfig:FindFirstChild("Burst") then burst = weaponConfig.Burst.Value end
	if weaponConfig:FindFirstChild("Shot") then shot = weaponConfig.Shot.Value end
	
	local stock = getAmmo(player, tool)
	if not stock and weaponConfig:FindFirstChild("MaxAmmo") and weaponConfig:FindFirstChild("MagSize") then stock = ammoSetup(player, tool) end
	if stock then mag, ammo = stock:GetAttribute("Ammo").X, stock:GetAttribute("Ammo").Y end
	print(fireRate, fireSpeed, range, spread, burst, shot, mag, ammo)
	return fireRate, fireSpeed, range, spread, burst, shot, mag, ammo
end

Let me know if I need to provide any more info.

Is the queryInfo variable in both localscripts referencing the same remote function?

Yes, it is. I’ve recently added code to make sure that it’s either one or the other scripts firing the function, and not both at the same time. It’s made no difference.

The problem is you cannot connect two different functions to the same remoteFunction as one will not work

The problem I’m having predates my adding the separate script with the separate function to the game.

Are you sure it is even firing by placing print statements before and after the :Invoke call?

1 Like

Do the prints on the Client side print?

“(from a game file where QueryInfo worked 100% of the time)”

Can you post that script please

A possible reason is that the functions inside the OnServerInvoke connection are not returning

The:
getConfig(tool)
and
getAmmo(player, tool)

I did exactly this in trying to figure out the problem before I posted the topic. The client-side prints would print, the server-side ones wouldn’t.

It’s identical to the “weapon system” sections of the script in OP.

Just tested with more prints. Not the reason.

Okay I made a quick test, with my own code and a Tool and it works as expected

This is a LocalScript

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Remote = ReplicatedStorage:WaitForChild('RemoteFunction')
	
local function newCharacter(character)
	
	local function NewTool(Tool: Tool)
		if not Tool:IsA("Tool") then return end
		
		local EquippedConnection
		local UnequipConnection 

		EquippedConnection = Tool.Equipped:Connect(function()
			local Data = Remote:InvokeServer()
			print(Data)
		end)
		UnequipConnection = Tool.Unequipped:Connect(function()
			print("Unequipped")
			EquippedConnection:Disconnect()
			UnequipConnection:Disconnect()
		end)
	end
	
	Character.ChildAdded:Connect(function(v)
		NewTool(v)
	end)
	for _,v in pairs(character:GetChildren()) do 
		NewTool(v)
	end 

end

LocalPlayer.CharacterAdded:Connect(newCharacter)
newCharacter(Character)

This is the Server script

game:GetService('ReplicatedStorage'):WaitForChild('RemoteFunction').OnServerInvoke = function(player)
	return "The time is: ".. os.time()
end

Try use this code as a starting point, and add onto it.

This ended up being the solution. I had a function call in another script that I didn’t consider that was using the remote function as well. Separating all the function calls out into different remote functions did the trick.

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