Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What () made for in :FireServer()?

I tried everything with () Currently I need to transfer data from local script into event script but functions is too complicated for me.


-- Local Script

local TextBox = script.Parent.Parent.TextBox
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
TextBox:GetPropertyChangedSignal("Text"):Connect(function()
event:FireServer(TextBox)
end)


-- Event  Script

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local function OnEvent()
print(TextBox.Text)
end

event.OnServerEvent:Connect(OnEvent)

is there even any way to get data from local and transfer to server using Event instead of Function?

2 answers

✓ Accepted answer

yes there is and i do not recommend at all using local functions instead you would simply do this

event.OnServerEvent:Connect(function(player,datasent)--datasent is just us naming a variable
	print(datasent)
end)

also always make sure to add player as first argument in server events since thats what roblox automatically pass as first argument

1 comment

0

It gives "TextBox", I typed in (player,TextBox), print(TextBox.Text). how to manage datasent? Do i have to type something in local script?

So here's how you would send data from a local script to a server:

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
--  I used event as the name since that's what you put. 
-- If you want to send the TextBox that's fine but you can just send the Text if that's all you are using.
TextBox:GetPropertyChangedSignal("Text"):Connect(fuction()
	event:FireServer(TextBox)
end)

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local function OnEvent(player, TextBox)
	print(TextBox.Text)
end

event.OnServerEvent:Connect(OnEvent)

-- OR

event.OnServerEvent:Connect(function(player, TextBox)
	print(TextBox.Text)
end)

So anytime you are dealing with Replicated Storage events you always want to have in your script the following:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local NameOfEventCanBeAnything = game.ReplicatedStorage:WaitForChild("WhateverYouNamedTheRemoteEventInYourReplicatedStorage")

An important note is you don't have to use WaitForChild. But it's whatever.

So after you included those in your local script you need to fire variables to the server like you did so:

NameOfEventCanBeAnything:FireServer(TextBox)

This actually passes two variables, player and TextBox. When you fire to the server player is always passed as the first parameter.

Now in the server you always need to have these:

local function TheFunctionYouWantToCall (player, VariableYouPassedIn)
WhateverYouNamedYourEventCanBeAnything.OnServerEvent:Connect(TheFunctionYouWantToCall)

The OnServerEvent:Connect listens for WhateverYouNamedYourEventCanBeAnything to recieve values from the local script and if it does it calls the function and passes those values into it.

So in your case the VariableYouPassedIn was TextBox. You don't have to put the name of the TextBox, it can be anything as long as it's in the right spot it becomes TextBox if that makes sense.

So what I mean is if you said FireServer(TextBox), the function in your server can be local function WhateverYouWantToCallIit(player, BLAH) and BLAH will be TextBox.

5 comments

0

what the point of local replicatedStorage if you're using game.ReplicatedStorage in second line of script and not just replicatedStorage, first line that you made is useless

0

If you look on the roblox Fire Event page it has that there. https://developer.roblox.com/en-us/api-reference/function/RemoteEvent/FireServer

0

I mean you are right, but I like to have that there just In case I create a remote event inside the script

0

its so cringey when you name a variable instead of just connecting it directly

0

It is not cringy. This is expected in any programming job you take outside of introduction courses. lol If you study in college they will teach this.

This discussion is preserved as it was. New answers, comments, and votes are closed.