InvokeServer not returning anything

I’m still working on an arrests/citations database. I’ve been debugging my GUI code, and it appears to error (silently, there’s no output about it) at this line.
databaseFE.GetArrestData:InvokeServer(lookup,"GetArrestRecordAmount")
It’s being invoked from the GUI inside the PlayerGui.

This is the code from databaseFE, which is a server script in ReplicatedStorage:

local databaseModule = require(script.Parent.DatabaseModule)

function script.GetArrestData.OnServerInvoke(plr,lookup,event)
	if event == "GetArrestRecords" then
		return databaseModule.GetArrestRecords(lookup)
	elseif event == "GetArrestRecordAmount" then
		return #databaseModule.GetArrestRecords(lookup)
	end
end

And here’s the relevant code from databaseModule, a module script also in ReplicatedStorage:

local database = {}

local arrestsDS = game:GetService("DataStoreService"):GetDataStore("Database","Arrests")

local keyprefix = "lcrecordid_"

-- retrieve the arrest records for the given player
function database.GetArrestRecords(plr)
	local key = keyprefix..plr.userId
	local record = arrestsDS:GetAsync(key)
	if record then
		return record
	else
		return {}
	end
end

Can anyone help me understand why the InvokeServer() is silently erroring?

2 Likes

Wait do you have the script running in ReplicatedStorage? For a script to run it needs to be in Workspace or ServerScriptService.

3 Likes

Server scripts don’t run in ReplicatedStorage.


Another issue could be an unhandled case. This is typically why you return at the end of an invoke, or any callback function, to ensure that something gets returned and doesn’t silently error or hang a script. The return at the end should be a default value or a nil to indicate that nothing could be returned.

local databaseModule = require(script.Parent.DatabaseModule)

function script.GetArrestData.OnServerInvoke(plr, lookup, event)
    if event == "GetArrestRecords" then
        return databaseModule.GetArrestRecords(lookup)
    elseif event == "GetArrestRecordAmount" then
        return #databaseModule.GetArrestRecords(lookup
    end
    return nil
end
5 Likes

Another error aside from your ServerScript not being in ServerScriptService (or even workspace for that matter) is that I think you may be calling an invoke wrong.
Invokes are supposed to be called like this (from my knowledge)

remotefunc.OnServerInvoke = function(player, blah, blah, blah)
    --code
end
1 Like

I had something just like this happening to me not too long ago. Aside from doing what colbert said, what fixed it for me was putting the RemoteFunction itself in ReplicatedStorage. For some reason, there would be no error but nothing would happen if the RemoteFunction was in some place like ReplicatedFirst.

1 Like

I’ll test all of your solutions when I get home. Thank you so much!

1 Like

I’ve moved the scripts from ReplicatedStorage to ServerScriptStorage. However, I now experience this error.

image

DatabaseFE is definitely in ServerScriptStorage, so I’m confused.

1 Like

LocalScripts and ModuleScripts required by LocalScripts cannot access anything in ServerStorage or ServerScriptService.

2 Likes

Im not sure if you got confused in your script, but there is ServerScriptService and ServerStorage

3 Likes

Nope, I wrote it correctly in the script but wrote it incorrectly here, LOL!

2 Likes

Then your problem is most likely what Sebastion said:

1 Like

Oh - that makes sense now! I was told server scripts won’t run in ReplicatedStorage, so do you have any suggestions as to where I should put the DatabaseFE script?

1 Like

You could put it in ReplicatedStorage or ServerStorage, it doesn’t matter. It matters more of where it’s required from.

1 Like

The script should go in ServerScriptService, the RemoteFunction should go in ReplicatedStorage. DatabaseModule should probably go in ServerStorage since the client won’t need it.

3 Likes

Now it just silently errors, for some reason. Doesn’t work.

This is the hierarchy.
image

1 Like

“Silently” and “errors” are two mutually exclusive terms. Does it just not work? Does it throw an error? Does it return an empty table? etc.

2 Likes

That’s my bad, I used the phrase improperly. What’s actually happening is that:

  • no error is being thrown; and
  • my print statements debugging the GUI functions module stop printing just before the line invoking GetArrestData.

Could you post the functions module?

1 Like
local module = {}  

local databaseGUI = script.Parent
local information = databaseGUI.Information
local search = databaseGUI.Search
local events = game:GetService("ReplicatedStorage")

local groups = {
	["LCSO"] = 4571364,
	["FHP"] = 4571359,
	["TPD"] = 4571365,
	["DHS"] = 4572104,
	["FG"] = 5015119,
	["LCFD"] = 4571367,
	["SWAT"] = 4830827,
	["Main"] = 4571345
}

function module.updateInformation(lookup)
	information.Visible = true
	search.Visible = false
	information.Age.Text = lookup.AccountAge
	information.Username.Text = lookup.Name
	print(1)
	information.UserID.Text = lookup.UserId
	print(2)
	information.PlayerImage.Image = game.Players:GetUserThumbnailAsync(lookup.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size60x60)
	print(3)
	information.Arrests.Text = "Arrests: "..events.GetArrestData:InvokeServer(lookup,"GetArrestRecordAmount")
	print(4)
	information.Citations.Text = "Citations: "..events.GetCitationData:InvokeServer(lookup,"GetCitationRecordAmount")
	for shortName,groupID in pairs(groups) do
		local clone = information.GroupScrollingFrame.Group:Clone()
		clone.ShortName.Text = shortName..":"
		clone.Rank.Text = lookup:GetRoleInGroup(groupID)
		clone.Visible = true
	end
end

The output only shows prints for 1, 2, and 3. Then it stops.

1 Like

The issue is that for some reason the server is failing to return a value, so the LocalScript is left waiting forever. Looking at it right now, the only thing I can think of is that “DatabaseFE” isn’t properly setting the callback for “GetArrestData.” Did you change function script.GetArrestData.OnServerInvoke to function game:GetService("ReplicatedStorage").GetArrestData.OnServerInvoke?

(Also sorry for taking so long to respond, for some reason I wasn’t notified that you responded.)

1 Like