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

Can someone help me with RemoteSeverEvents?

Asked by 4 years ago

So I for the life of me cannot figure out remote server events, and I have simply avoided it by limiting my use of local scripts and taking the sometimes long way around. I have now run into a problem I think is only possible through firing an event. I want someone to be able to type out how much Ore they would wish to sell. inside the TextButton I have a local script which reads

local repstor = game:GetService("ReplicatedStorage")
local remote = repstor:WaitForChild("StoreType")

local function changed()
    local texty = ""..script.Parent.Text..""
    remote:FireServer(texty)
end
script.Parent.Changed:Connect(changed)

I have of course a RemoteEvent (do i need a RemoteFunction?) named StoreType Inside the RemoteEvent I have a standard script which reads

local repstor = game:GetService("ReplicatedStorage")
local remote = repstor:WaitForChild("StoreType")

local function changed()
    local texty = ""..script.Parent.Text..""
    remote:FireServer(texty)
end
script.Parent.Changed:Connect(changed)

I have rewritten this and tried so many alternatives on my own, I even understand how clients vs servers work and I know i can transfer data (which is what i tried to do with "texty"), I just never am able to successfully fire/call thing.

Thanks!

1 answer

Log in to vote
1
Answered by
nievadev 112
4 years ago
Edited 4 years ago

This would be fixed just explaining you how remote events work, and explaining you properly. The documentation might be confusing at times.

Let's say, the client clicks on a button that makes the server create a block.

For this, you would use a Remote Event.

So, you create a remote event, called for example CreateBlock, inside the ReplicatedStorage. Why ReplicatedStorage, you might ask? Because it's a place both the client and server has access to. It's read only for clients, meaning no local scripts can change its contents.

To make this remote event work, you would have to define its behaviour in a server script or a local script. In this case, you define its behaviour in a server script since the server script is gonna do the work.

Note: when you define the behaviour of a remote event in a server script, ALWAYS the first parameter of the function is gonna be the player who fired the event. Be aware of that.

In the server script, then:

local remoteEvent = game:GetService('ReplicatedStorage').CreateBlock

-- Defining its behaviour - what it's gonna do
remoteEvent.OnServerEvent:Connect(function(player)
    local part = Instance.new('Part', workspace)
end)

Now, the remote event knows exactly what it must do: create a block. We defined it already in the server script.

Now, what is left, is to define WHEN it's gonna be fired from the client. Let's say, then. that it fires every time the client presses 'F' key

local remoteEvent = game:GetService('ReplicatedStorage').CreateBlock

game:GetService('UserInputService').InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.F then
        remoteEvent:FireServer()
    end
end)

And that'd be it. Very simple, right? If you understand that, you would easily understand the rest of the documentation on remote events :)

Glad to help you brother, and remember, if this is useful for you, accept the answer!

0
You should've done the client first before doing the server. Dovydas1118 1495 — 4y
0
No. I think it's easier to understand that way. Also, his problem in this question was that he didn't understand remote server events - so I explained them. If he understands this explanation, he'll understand the rest in the documentation, I think. nievadev 112 — 4y
Ad

Answer this question