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

Why doesn't this PlayerAdded function work?

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.

3 comments

0

Client - Server.

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

0

What are you trying to do with this remote event? Also where are your scripts located? 

2 answers

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

game.Players.PlayerAdded:Connect(function(plr)
 -- Remote Event
end)

4 comments

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.

0

That didn't work.

0

Could you give me the error if it outputs one?

0

There is none.

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.

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