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

RemoteFunction Not Detecting InvokeServer?

Hi, I've been making a keybind system for a game, and I'm using a RemoteFunction to fetch/save keybind data. However, it isn't detecting my InvokeServer calls. The odd thing is, it works like 1/6 of the time. You might notice the print("Hook") in the server script, and it never prints

This is part of my LocalScript that invokes the function

local Keybinds

repeat wait() -- I tried this to get the RemoteFunction to respond
	spawn(function()
		print("Trying For Data") -- Infinitly prints
		Keybinds = ReplicatedStorage.Funky:InvokeServer("Keybinds")
	end)
until Keybinds

This is the server script that is controlling the RemoteFunction (I don't know if it matters but I have multiple scripts listening to the same RemoteFunction)

local ds = game:GetService("DataStoreService"):GetDataStore("Keybinds","idlkfvjnh")
local cache = require(script.Cache)

function game.ReplicatedStorage.Funky.OnServerInvoke(plr,action,data)
	print("Hook")
	if action == "Keybinds" then
		if not data then
			print("Fetch")
			local save = ds:GetAsync(plr.UserId)
			if save then
				return save
			else
				return game.HttpService:JSONEncode({"W","A","S","D"})
			end
		else
			print("Data")
			if #game.HttpService:JSONDecode(data) == 4 then
				cache[plr.UserId] = {plr.UserId,data}
			else
				error("Keybind Localscript Did Not Send 4 Values")
			end
		end
	end
end

while wait(8) do
	for i,v in pairs(cache) do
		ds:SetAsync(v[1],v[2])
		print("Saved Cache Data For "..game.Players:GetNameFromUserIdAsync(v[1]))
		table.remove(cache,i)
	end
end

2 answers

.OnServerInvoke() or .OnClientInvoke() are callbacks; you can't connect it to a function. So you assign it to a function.

Example;

--Server script
local remote_function = game.ReplicatedStorage:WaitForChild("RemoteFunction")
remote_function.OnServerInvoke = function(plr)
	return plr.Name
end

--LocalScript
local remote_function = game.ReplicatedStorage:WaitForChild("RemoteFunction")

print(remote_function :InvokeServer(game.Players.LocalPlayer) --prints the player's name

Not a practical example, but I hoped it got the point across! For more on callbacks, go here

2 comments

0

function RemoteFunction.OnServerInvoke works too doesn't it

0

still doesn't work

Ok, the issue was that I had 3 scripts listening to OnServerInvoke on the same RemoteFunction

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