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

Why doesn't this PlayerAdded function work?

Asked by 5 years ago

Calls the remote, which I believe where the issue is.

game.Players.PlayerAdded:Connect(function()
game.ReplicatedStorage.Begin:FireServer()
end)  

This is what the RemoteEvent executes.

game.ReplicatedStorage.Begin.OnServerEvent(function()
print('Hello')
end)

I get this should be a simple fix, but I don't get why it is not executing correctly.

0
Client - Server. Untacked 10 — 5y
0
I believe the issue here is that the local script runs after the player joins the server, so the PlayerAdded event would only fire after a different player joins the server MegaManSam1 207 — 5y
0
What are you trying to do with this remote event? Also where are your scripts located?  MegaManSam1 207 — 5y

2 answers

Log in to vote
0
Answered by
KDarren12 705 Donator Moderation Voter
5 years ago
Edited 5 years ago

Could you try to use this function and tell me how it works?

game.Players.PlayerAdded:Connect(function(plr)
 -- Remote Event
end)
0
I think the reason it isn't working is because you didn't specify the inside of a function; then again, I could be wrong. KDarren12 705 — 5y
0
That didn't work. Untacked 10 — 5y
0
Could you give me the error if it outputs one? KDarren12 705 — 5y
0
There is none. Untacked 10 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I believe what is happening is that the player joins before the event loads in. If this is the case, use WaitForChild() to wait for the event to load in:

Local Script

local event = game:GetService("ReplicatedStorage"):WaitForChild("Begin") -- This is the remote event
game.Players.PlayerAdded:Connect(function()
    event:FireServer()
end)

Server Script

game:GetService("ReplicatedStorage"):WaitForChild("Begin").OnServerEvent:Connect(function()
    print("Hello")
end)

You should always use WaitForChild() to wait for something to load, especially a RemoteEvent. Players tend to be one of the first things to load in, and sometimes they load in before other Instances.

Answer this question