When you call Module.GetDevice(plr) in a script, my Module sends a remote to the client. The Client then sends it back to the server with a string that matches the given Players’s Device.
It seems to work fine on the Client as well as in the Module (I checked by printing out the device’s type)
Module:
local replicated = game:GetService("ReplicatedStorage")
local remotes = replicated.Module.Remotes
local Players = game:GetService("Players")
local module = {}
module.GetDevice = function(plr)
remotes.Main:FireClient(plr, "DeviceCheck")
remotes.Main.OnServerEvent:Connect(function(plrFrom, device)
if plrFrom ~= plr then return end
print("Module: "..device) -->Module: Desktop
return device
end)
end
return module
Script:
local Players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local remotes = replicated.Module.Remotes
local Module = require(script.Parent.Modules.MainModule)
Players.PlayerAdded:Connect(function(plr)
print(Module.GetDevice(plr)) --> -
end)
As shown above, the Module prints what’s expected but the returned Value (device) seems to be “-”
No Errors. No Warnings
Any Ideas why this isnt’t working? Thanks in advance
Connected functions do not return to outer functions. If you want to return the first value received from an event use :Wait() like so, this will make your function async and yield.
module.GetDevice = function(plr)
remotes.Main:FireClient(plr, "DeviceCheck")
local player, device = remotes.Main.OnServerEvent:Wait()
return device
end
EDIT: This looks like you are trying to fake a remote function, just use a remote function. Everyone will tell you they are dangerous but this is just as dangerous if not more. If your client errors you will have a impossible to fix stalled thread instead of an error to act on or pcall.
Well even tho I did not fully understand this question I think that’s what you mean:
Server - Client - Server
I know this is not recommended to do but checking the device is not that important anyway: I use return only to read what the remote gave me so: Let’s say the player is using a desktop:
The remote would send a request to the client. → The Client will Fire the remote once again - this time with the Players device (which is, as you said, a string) → Now since I need the device in my Script and not the Module I do return device This should get get the device to the Script, right?
I would move the :Connect() to wherever you create the remote event. One way you could do this is having a centralized place where you store all users devices for the remainder of their stay.
I would argue using the modified version I provided is safer than this. While it is far from an elegant solution, it still circumvents most/all issues with remote functions and the initial pseudo function.
You’re snippet is certainly the best solution to the author’s use case. Remote functions can almost always be avoided and Server - Client - Server remote functions are extremely rare.
Caching solutions like yours introduce race conditions which are often terrible to debug but again, undoubtingly best for the author’s script.