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

How come game.Players.PlayerAdded doesn't work in my script?

Asked by 9 years ago

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)
0
It's because the script is in server storage that it will not work! M39a9am3R 3210 — 9y

3 answers

Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago

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)
0
The edits you made seem to be all well and good, but the event isn't firing when someone joins the game to begin with. Why do you think that is? Novaulis 15 — 9y
0
Because the event only fires when the property is changed. Validark 1580 — 9y
0
Not that event, the PlayerAdded event. Novaulis 15 — 9y
0
Scripts don't run in ServerStorage. Put the script in ServerScriptService Validark 1580 — 9y
View all comments (2 more)
0
After a couple of little edits, the script runs in ServerScriptService now. Thanks for all the help! Novaulis 15 — 9y
0
Yup! Validark 1580 — 9y
Ad
Log in to vote
0
Answered by
sigve10 94
9 years ago

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

Log in to vote
0
Answered by 9 years ago

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)

Answer this question