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

How can I take a variable from a localscript and put it into a server script?

Asked by 3 years ago

I have been having a lot of trouble getting a variable in my local script to my server script from teleport data is there any way to do this?

0
remote events Subsz 366 — 3y
0
Remote events and remote functions. radiant_Light203 1166 — 3y
0
You could also use ModuleScripts, but RemoteEvents are preferred for data transfer. killerbrenden 1537 — 3y
0
Could you post an example on how to use remote events to do this krimsinTV -5 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

The best way to do this is to use bindable events,

So say your workspace looked something like this

Workspace
    -Script1
    -Script2
        -BindableEvent

What we would want to do to transfer a variable from Script1 to Script2 which has a remove event,

In Script1 our code would look something like this

WaitForVariable = game.Workspace:WaitForChild("Script2") --Here we are waiting for Script2 to exist in the workspace.
myVariable = 123 --Here we are defining the variable
game.Workspace.Script2.BindableEvent.Fire(myVariable) --Here we are activating the event

In Script2 We would need to get that data that we just sent over,

function DataTransfer(Data) 
    myVariable = Data
end
script.Event.Event:Connect(DataTransfer)

Now if we were to print myVariable it should print 123.

Ad
Log in to vote
0
Answered by 3 years ago

Or you can make Remote events

Examples:

Local Script:

workspace.ChildAdded:Connect(function(add)
    script.Parent.Remotes.Child.Added:FireServer(add)
end)

Server Script:

script.Parent.Child.Added.OnServerEvent:Connect(function(add)
    if add then
        if add:IsA("Part") then
            add.Shape = "Ball"
        end
    end
end)

Answer this question