Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Should I use a RemoteEvent or a RemoteFunction for a GUI?

Asked by 7 years ago

Hello all,

I have a question here. I've recently turned on FilteringEnabled in my place so I'm changing the scripts. I am confused if I should use a RemoteEvent or RemoteFunction. What I want to happen is that whenever the button in the gui is pressed the RemoteEvent/Function is passed on and that should give the player (which will be in a textbox) an item. But I do not know whether I should use a RemoteEvent or Function and how the code in the RemoteEvent/Function will work.

PS: I have already read all wiki threads about this and tried everything and it still does not work. I have also already tried changing to a RemoteFunction. In case you're wondering, nothing is showing up in the output.

Here's the code in the gui:

script.Parent.confirm.MouseButton1Click:connect(function(player)
    game.ReplicatedStorage.GiveRoom:FireServer(player)
end)

Here's the code in the RemoteEvent:

script.Parent.OnServerEvent:connect(function(player)
    local reception = player:WaitForChild('PlayerGui').Reception.FrameA.CheckIn
    if reception.errormsg.Text == "Error: Room not available" then
        warn("Room not available") else
        if game.Players[reception.username.Text] then       
        game.ServerStorage.ReceptionSystem.RoomsFolder["Room"..reception.roomnum.Text].Value = true
        local newVal = Instance.new("StringValue", game.ServerStorage.ReceptionSystem.CheckedIn)
        newVal.Name = "Room"..reception.roomnum.Text
        newVal.Value = game.Players[reception.username.Text].Name
        local newKey = game.ServerStorage.ReceptionSystem.Card:Clone()
        newKey.Parent = game.Players[reception.username.Text].Backpack
        newKey.Name = "Room "..reception.roomnum.Text
        newKey.CardNumber.Value = reception.roomnum.Text
        local newKey2 = game.ServerStorage.ReceptionSystem.Card:Clone()
        newKey2.Parent = game.Players[reception.username.Text].StarterGear
        newKey2.Name = 'Room '..reception.roomnum.Text
        newKey2.CardNumber.Value = reception.roomnum.Text
        reception.confirm.Text = "Checked in!"
        reception.roomnum.Text = "Enter room number"
        reception.username.Text = "Enter username"
        wait(0.5)
        reception.confirm.Text = "Check-in"
        end
    end 

reception.roomnum.Changed:connect(function()
    reception.errormsg.Text = " "
    if game.ServerStorage.ReceptionSystem.CheckedIn["Room"..reception.roomnum.Text] then
        reception.errormsg.Text = "Error: Room not available"
    end
end)
end)

Thanks in advance!

0
Deploy some prints, see what isn't running that should be. Azarth 3141 — 7y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

I would use a RemoteEvent to do everything you're doing with the Keys and keep the GUI stuff to the LocalScript. If you need anything from the keys in the local script, use a RemoteFunction instead to return it. Remember, anything you do with a LocalScript with FE is only going to be visible to you.

You use a RemoteFunction when you want the script to yield (wait) until the function returns something. For instance, what if I made a Local script that needed something that only could be accessed by a Server script, like something in SeverStorage? I would need to yield until that object is returned to continue on with the script, else it'll return nil because it continued without waiting for the returned object.

In summary via Wiki "A RemoteFunction sends a request and then waits for a response, while RemoteEvents just send a request."

You can visualize this by structuring your scripts like so and trying each of these examples.

RemoteFunction example

--Inside the LocalScript

local RemoteFunction = script:WaitForChild('RemoteFunction')
local now = tick()
local returned = RemoteFunction:InvokeServer()
print(returned, tick() - now) -- prints what it returned and how long it yielded until something was returned.
-- Inside the script

local RemoteFunction = Instance.new("RemoteFunction", script.Parent)
function RemoteFunction.OnServerInvoke()
    for i = 1, 5 do
        wait(.5)
    end
    return 'bagel'
end

RemoteEvent example

--Inside the LocalScript

local RemoteEvent = script:WaitForChild('RemoteEvent')
local now = tick()
local returned = RemoteEvent:FireServer()
print(returned, tick() - now) -- prints nil and some huge value, because it didn't yield at all.
-- Inside the script

local RemoteEvent = Instance.new("RemoteEvent", script.Parent)
RemoteEvent.OnServerEvent:connect(function()
    for i = 1, 5 do
        wait(.5)
    end
    return 'bagel'
end)
0
I'm not sure I'm doing this correctly. Is the script supposed to go inside of the RemoteEvent? TheEpicCooldeal 12 — 7y
Ad

Answer this question