What I'm trying to do is make a gui that'll display the value of a NumberValue in ServerStorage, and since they're on the client and the other's on the server respectively, I need to do some fancy things with RemoteFunctions to make it work. I got some code from the wiki and spliced it together with what I need, and now the function isn't even running when a player is added.
A little more info: The game isn't the start place and it's in a normal script parented to a NumberValue under ServerStorage. I already checked, and script.Disabled = false.
game.Players.PlayerAdded:connect(function(player) player:WaitForChild("Backpack") local event = Instance.new("RemoteEvent") event.Parent = player.Backpack event.Name = "RoomChanged" end) script.Parent.Changed:connect(function(Property) local NewNumber = script.Parent[Property] game.Workspace.RoomChanged:FireClient(NewNumber) end)
You were misusing FireClient. First of all, you didn't properly identify which RemoteEvent you wanted to fire. Secondly, you didn't tell the script **which ** client it needs to Fire. Try the following:
game.Players.PlayerAdded:connect(function(player) local event = Instance.new("RemoteEvent", player:WaitForChild("Backpack")) event.Name = "RoomChanged" script.Parent.Changed:connect(function(Property) -- Writing it this way tells the script to create a separate connection for each client event:FireClient(player, script.Parent[Property]) end) end)
I am assuming you are doing this in studio, am I right? Well, the thing is that in studio the player is loaded before the scripts. This should probably work as it should online, but if you need to use it in studio (for debugging or something else), you can use this modified script of yours:
playerAdd(player) --Renamed your function local event = Instance.new("RemoteEvent", player:WaitForChild("Backpack")) event.Name = "RoomChanged" script.Parent.Changed:connect(function(Property) -- Writing it this way tells the script to create a separate connection for each client event:FireClient(player, script.Parent[Property]) end) end game.Players.PlayerAdded:connect(playerAdd) -- Binding the function to when a player joins the game for i,v in pairs (game.Players:GetPlayers()) do --Checking for all players that are in game playerAdd(v) --Running playeradded for all players in game end
This script checks the server for players that are in it, and runs your function for it!
I hope this helped
Let's start at the obvious issues
Counterintuitively, NumberValues use a different Changed to the base Instance class
What this means for you is that your code for the NumberValue
should become
script.Parent.Changed:connect(function(NewNumber) game.Workspace.RoomChanged:FireClient(NewNumber) end)
Scripts don't run in ServerStorage
That has a simple fix, which is to move your script out of the NumberValue
and to access it statically, or to move your NumberValue
to somewhere that scripts will run.
You're looking for the wrong event
The Player has a new RemoteEvent
in them, but your script is still looking for the one in Workspace
. Consider changing your script around so that you're not making a new RemoteEvent in each Player, and instead each Player is listening to the one in Workspace
.
You're not telling the RemoteEvent which Player to fire to
This one can be solved easily, because your code's intent is fairly obvious. Instead of using :FireClient(p,...)
, use :FireAllClients(...)
script.Parent.Changed:connect(function(NewNumber) game.Workspace.RoomChanged:FireAllClients(NewNumber) end)