Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

RemoteFunction from Server to Client concerns?

Asked by
Despayr 460 Moderation Voter
6 months ago

The Roblox Developer Site states that a Remote Function that uses the server to retrieve information from the client is discouraged due to the reason that if the Client disconnects before it can retrieve data, the server will hang forever.

I was going to use the Remote Function so that the server could retrieve the mouse position from the client, but I'm worried that the client could disconnect and leave the server hanging. Would there be any solution to this problem? I'd rather not have to rely on many remote events.

The reason that I cannot send the mouse position through the remote (tool for now) is because there is a time difference between when the remote is activated, and when I want the mouse position just in case it changes

Example

Server

local tool = script.Parent
local remotefunction = tool:WaitForChild("RemoteFunction")
local part = nil

local playerservice = game:GetService("Players")

tool.Activated:Connect(function()
    part = Instance.new("Part")
    part.Anchored = true
    part.CFrame = tool.Parent.PrimaryPart.CFrame
    part.Parent = tool.Parent
end)

tool.Deactivated:Connect(function()
    if part == nil then return end

    local player = playerservice:GetPlayerFromCharacter(tool.Parent)
    if not player then return end
    local mousepos = remotefunction:InvokeClient(player)

    part.CFrame = CFrame.new(part.Position, mousepos)
end)

Client

local remotefunction = script.Parent:WaitForChild("RemoteFunction")

remotefunction.OnClientEvent = function()
    local mouse = game.Players.LocalPlayer:GetMouse()
    return mouse.hit.p
end

Once again, according to the roblox dev site, doing this is discouraged. I'm looking for a way to safely retrieve data.

0
It's OnClientInvoke* Ziffixture 5655 — 6mo

1 answer

Log in to vote
1
Answered by 6 months ago

Just swap out your RemoteFunction for a RemoteEvent.

You will have to rewrite your server side a bit as you can no longer expect a direct result, instead the result will have to come from a OnServerEvent. This way you basically replicate a RemoteFunction but you manually tell the client to fire the event on the server.

0
It will have to do. I'll have the server fire the event to the client, the client will then fire to the server, and finally, the server will add the mouse position to a table Despayr 460 — 6mo
Ad

Answer this question