I can understand why you think it would work, in order to understand why it doesn’t work you’ll need to understand the Roblox Client-Server model.
More info: Developer Hub
Basically, the server is well, the server, and the client is a player, so there can be multiple clients connected to a server.
LocalScripts are run on each client, while Normal/Server scripts run on the server.
Now, the reason why the code isn’t working is that variables are not shared across client and servers, since they run on separate computers.
To solve this problem, Roblox has RemoteEvents
and RemoteFunctions
, these objects are used to communicate from server to client, or from client to server.
More info: Developer Hub
In this case you would use a RemoteEvent
and send a message to all clients.
See article linked above for the difference between Remote Events and Functions
Here is some simple example code that sends a message to all players/clients in the server.
-- Must be run on a Server Script
local myRemoteEvent = -- remote event here --
-- Run :FireAllClients() to send a message to all clients
-- In this case, we are sending the string "my message"
-- but it can be anything.
myRemoteEvent:FireAllClients("my message")
Alternate Solution
A simpler solution is the use a NumberValue
or attribute, since changes of NumberValues or attributes from the server will replicate to all other clients, replication is basically the server telling all other clients that this has changed.
And another thing is, avoid using Global Variables, always try to use local
instead.