Returning Data from a RemoteEvent?

  1. What do you want to achieve?
    I’d like a return some information from a dictionary via a RemoteEvent.

  2. What is the issue?
    It’s not returning a value (see error)
    image

  3. What solutions have you tried so far?
    I’m not really sure what else to try, looked through some other posts, but haven’t found much relating to this.

The Server Script

game.ReplicatedStorage.RemoteEvents.RequestData.OnServerEvent:Connect(function(Player, Data)
	if Player then
		-- Data in leaderstats
		if Data == "Level" then
			return(ServerData[Player].leaderstats.Level)
		elseif Data == "Rebirths" then
			return(ServerData[Player].leaderstats.Rebirths)

			-- Data in hiddenstats
		elseif Data == "Coins" then
			return(ServerData[Player].hiddenstats.Coins)
		elseif Data == "OwnedTags" then
			return(ServerData[Player].hiddenstats.OwnedTags)
		elseif Data == "EquippedTag" then
			return(ServerData[Player].hiddenstats.EquippedTag)
		elseif Data == "OwnedTrails" then
			return(ServerData[Player].hiddenstats.OwnedTrails)
		elseif Data == "EquippedTrail" then
			return(ServerData[Player].hiddenstats.EquippedTrail)
		end
	end
end)

Just some context for the above. ServerData contains all of the data for the players in the current server.

The Local Script

while wait() do
	script.Parent.Text = tostring(game.ReplicatedStorage.RemoteEvents.RequestData:FireServer("Coins"))
end

Any input on this would be much appreciated :slightly_smiling_face:

1 Like

You have to use RemoteFunctions.

3 Likes

I’m not sure if this’ll help, but I believe you need to use a RemoteFunction and invoke it via InvokeServer

game.ReplicatedStorage.RemoteEvents.RequestData.OnServerInvoke = function(Player, Data)
	if Player then
		-- Data in leaderstats
		if Data == "Level" then
			return ServerData[Player].leaderstats.Level
		elseif Data == "Rebirths" then
			return ServerData[Player].leaderstats.Rebirths

			-- Data in hiddenstats
		elseif Data == "Coins" then
			return ServerData[Player].hiddenstats.Coins
		elseif Data == "OwnedTags" then
			return ServerData[Player].hiddenstats.OwnedTags
		elseif Data == "EquippedTag" then
			return ServerData[Player].hiddenstats.EquippedTag
		elseif Data == "OwnedTrails" then
			return ServerData[Player].hiddenstats.OwnedTrails
		elseif Data == "EquippedTrail" then
			return ServerData[Player].hiddenstats.EquippedTrail
		end
	end
end
while wait() do
	script.Parent.Text = tostring(game.ReplicatedStorage.RemoteEvents.RequestData:InvokeServer("Coins"))
end

You’d of course hae to change the RemoteEvent RequestData into a RemoteFunction and change some stuff around

3 Likes

Ok this makes a lot of sense, you have saved me from a lot of head scratching. Thank you for your help!

1 Like

you might see some lag later down the line since you’re using a while wait do and calling server invoke. thats alot of requests. You could just check on the server any time their coins are changed and fire an event to the client to change the text. also on respawn so it updates first thing.

2 Likes

Thank you for this, I’ll change this to make it more efficient.