This is part of a longer script, but I've narrowed down that the problem is occurring here.
local player = game.Players.LocalPlayer local character = player.Character
When I run this with the "Play" button, it works fine. But when I run a server test with 2+ players, it stops on the line "local character = player.Character" line. Does local character not work in servers or is there another method I should use?
The Character
of a player loads in a little bit after the player object itself loads, and thus when the localScript first loads, the Character
is nil. A common way to get around this is to use the following code:
local char = player.Character or player.CharacterAdded:wait()
With the or
in there, when running this script, the game will first check if player.Character
is a thing, and if it is, will return player.Character,
however, if player.Character
is not a thing yet (hasn't loaded yet) it will run player.CharacterAdded:wait()
and wait for the character before returning it.
Since you didn't specify in your question, I assumed you used a LocalScript,
if you used a ServerScript
then you can't use LocalPlayer,
because it will return nil
instead of a player object. This occurs because there is one server, and many clients. LocalScripts
are run on the client, which let's you use LocalPlayer,
however, ServerScripts
(obviously) run on the server, and don't have a LocalPlayer
to reference.
This works when using solo play, because (at least I don't think) studio doesn't run a server in the background while you play as the client. Although, I believe studio does recognize some things that shouldn't be, as, in my experience, LocalScripts
do not run in the workspace during solo play, which is how they should work.