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

GUI not updating when value has changed?

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

✓ Accepted answer

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.

1 comment

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.

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