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

How come when I try to send data from a ServerScript to a LocalScript it won't work?

Asked by 3 years ago
Edited 3 years ago

--//Description\--

So I have 2 scripts both named Version. I will be calling them Version1 and Version2. So Version1 is a ServerScript, and it fires an Event named VersionEvent. Then Version2 is a LocalScript that grabs the event when it is fired, and it collects a piece of data called "placeVersion" aka it gets the server's current version, but for some reason, it won't do that what's happening?

--//Version1\--

local placeVersion = game.PlaceVersion

game.ReplicatedStorage.VersionEvent:FireAllClients(function(placeVersion)
    print(placeVersion)
end)

--//Version2\--

game.ReplicatedStorage.VersionEvent.OnClientEvent:Connect(function(placeVersion)
    script.Parent.Value = placeVersion
    print("Value Set.")
end)

--//P.S\--

Version1 is parented to ServerScriptService.

Version2 is parented to a TextLabel..

0
Why are you putting a function in the FireAllClients? MegaSpaghett 42 — 3y

2 answers

Log in to vote
1
Answered by
Wiscript 622 Moderation Voter
3 years ago

The first issue is you are parsing a function as a parameter when you call FireAllClients()

The second issue is you are trying to change the property Value in a TextLabel. You can see every property of a TextLabel here

When calling FireAllClients() you need to put inside the brackets () a list of arguments for the client to receive. This will be received by every client. Therefore, to do what you're trying to achieve it would look something like this:

Version1

local placeVersion = game.PlaceVersion

game.ReplicatedStorage.VersionEvent:FireAllClients(placeVersion)

Version 2

game.ReplicatedStorage.VersionEvent.OnClientEvent:Connect(function(placeVersion)
    script.Parent.Text = placeVersion -- assuming you want to change its text
    print("Value Set.")
end)

See, the difference between Version1 and Version2 is that V1 is a procedure (function with no return value), whereas V2 is an event. That is why we connect the event to a function. It means "Once v2 is called, connect it to this function (run it)". And V1 saying :FireAllClients() simply calls the event.

Another thing to note is that you don't need to use all this networking just to get the game's version. It is accessible from the client too.

Here is an example taken from the wiki on how to do what you're trying to achieve.

local StarterGui = game:GetService("StarterGui")

-- create a screenGui
local versionGui = Instance.new("ScreenGui")

-- create a textLabel
local textLabel = Instance.new("TextLabel")

-- position in the bottom right corner
textLabel.Position = UDim2.new(1, -10, 1, 0)
textLabel.AnchorPoint = Vector2.new(1, 1)
textLabel.Size = UDim2.new(0, 150, 0, 40)

-- configure the text settings
textLabel.BackgroundTransparency = 1
textLabel.TextColor3 = Color3.new(1, 1, 1)
textLabel.TextStrokeTransparency = 0
textLabel.TextXAlignment = Enum.TextXAlignment.Right
textLabel.TextScaled = true

-- display the version number
local placeVersion = game.PlaceVersion
textLabel.Text = string.format("Server version: %s", placeVersion)
textLabel.Parent = versionGui

-- parent to the StarterGui
versionGui.Parent = StarterGui

Best of luck!

<please accept my answer if I have answered your question>

0
Thank you for the help :) I read the wiki some time before I made this thread and I was confused thank you for clarifying that I can call it from the client! vincentthecat1 199 — 3y
Ad
Log in to vote
0
Answered by
achdef 0
3 years ago
Edited 3 years ago

You should not put a function in FireAllClients, correct me if I am wrong.

0
FireAllClients* achdef 0 — 3y
0
this should be a comment, not an answer Wiscript 622 — 3y

Answer this question