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

GUI not updating when value has changed?

Asked by 4 years ago

So, My script runs the code below when a player joins the game

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local status = replicatedStorage:WaitForChild("Status")
local minPlayers = 1

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        if #game.Players:GetPlayers() >= minPlayers then
            print(#game.Players:GetPlayers())
            status.Value = player.Name.." has joined the game"
        end
    end)
end)

"Status" is in the replicated storage and is used as the text for the TextLabel in my GUI. Here is the local script controlling that textlabel:

local replicatedStorage = game:GetService("ReplicatedStorage")
local status = replicatedStorage:WaitForChild("Status")
local label = script.Parent:WaitForChild("TextLabel")

status:GetPropertyChangedSignal("Value"):Connect(function()
    print("status changed")
    label.Text = status.Value
end)

Summary: When status' value has changed, update the textlabel

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

It's not working because simply the "Status" value is created on the server, and so the client will not be able to detect or read changes to it. Good news is there is a much better way to do this which involves the use of RemoteEvents which are used to communicate between the client and server. Try the below.

SERVER (SCRIPT)

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local minPlayers = 1

local status = Instance.new("RemoteEvent")
status.Parent = game.ReplicatedStorage
status.Name = "Status"

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        if #game.Players:GetPlayers() >= minPlayers then
            print(#game.Players:GetPlayers())
            status:FireAllClients(player.Name.. " has joined the game!")
        end
    end)
end)

CLIENT (LOCALSCRIPT)

local replicatedStorage = game:GetService("ReplicatedStorage")
local status = replicatedStorage:WaitForChild("Status")
local label = script.Parent:WaitForChild("TextLabel")

status.OnClientEvent:Connect(function(status)
    print("status changed")
    label.Text = status
end)

Please take the time to read up about RemoteEvents and RemoteFunctions for client-server and server-client communication.

0
Thanks! I was going to use remote events but I thought that since ReplicatedStorage is accessible by both server and client I could just use that instead. ApexBrick 59 — 4y
Ad

Answer this question