RemoteEvent returning nil

Hello. I am currently scripting a speedrun leaderboard but I have run into a problem with remote events. I have a server script for the leaderboard and a local script with the timer information. Currently, the server script is firing a remote event to which the local script receives and supposedly should return a string, however, it returns nil.

As you can see below the error, the time is being printed but not returned. Here are the functions.

Server script:

new.Value.Text = game.ReplicatedStorage.ReadableTimer:FireAllClients(d[2]/1000)

Local script:

game.ReplicatedStorage.ReadableTimer.OnClientEvent:Connect(function(timertime)
	print(readabletime(timertime))
	return readabletime(timertime)
end)

What did I do wrong?

Use a RemoteFunction instead, RemoteEvents don’t return anything

When I use a RemoteFunction, I get a new error

Show the script

I’m not really sure what you’re trying to do here involving invoking all clients

you may forgot change method.You should use “RemoteFunction:InvokeClient(player,…)”

Local Script function:

local function readabletime(timer)
	return string.format("%02d:%05.2f", timer/min, timer%min)
end
Readabletimer.OnClientInvoke = readabletime

Snippet of leaderboard code from server script:

for a, b in ipairs(top) do
		local userid = b.key
		local points = b.value
		local username = "[Failed To Load]"
		local s, e = pcall(function()
			username = game.Players:GetNameFromUserIdAsync(userid)
		end)
		if not s then
			warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		table.insert(data, {username, points, image})
		ui.Parent = script
		scrollingframe:ClearAllChildren()
		ui.Parent = scrollingframe
		for number, d in pairs(data) do
			local name = d[1]
			local image = d[3]
			local color = Color3.new(1,1,1)
			if number == 1 then
				color = Color3.new(1,1,0)
			elseif number == 2 then
				color = Color3.new(0.9,0.9,0.9)
			elseif number == 3 then
				color = Color3.fromRGB(166, 112, 0)
			end
			local new = sample:Clone()
			new.Name = name
			new.LayoutOrder = tonumber(number)
			new.ImageLabel.Image = image
			new.ImageLabel.Place.Text = number
			new.ImageLabel.Place.TextColor3 = color
			new.PName.Text = name
			new.Value.Text = game.ReplicatedStorage.ReadableTimer:InvokeClient(d[2]/1000)
			new.Value.TextColor3 = color
			new.PName.TextColor3 = color
			new.Parent = scrollingframe
		end
		wait()
		scrollingframe.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)
	end

That is what I was planning to do, but I have no idea how to get the player object from my current server script.

Remote events do not have a callback (meaning they do not return, just send a message). You’ll have to use Remote Functions instead.

Remote Functions don’t have a “InvokeAllClients” function, so you’ll have to loop through all the players and individually invoke the client.

Could look something like this on the server:

for i,v in game.Players:GetPlayers() do
   local result = game.ReplicatedStorage.ReadableTimer:InvokeClient(v, d[2]/1000)
   new.Value.Text = result
end

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