I’m trying to define a part in the character in a ServerScript. I’m aware of this being used to get the character and the part within it:
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Part = Character:WaitForChild("Part")
-- The actual part is not called Part, I just called it Part here for simplicity
end)
end)
The problem with this for me is that I want to use the part outside of this function - I don’t think it’s a good idea to put the stuff I want to use the part of the character in inside of this. For reference, this is in a ModuleScript being called by a ServerScript in the workspace.
Really, all I want is a way to have the whole PlayerAdded stuff work while also being able to define the part of the character like you would be able to with LocalPlayer:CharacterAdded:Wait().
If you want the part in a module script, then you could write all the code which need to be done for the character inside CharacterAdded().
Simply using a variable like @bikereh suggested is a good solution, as long as you don’t need to deal with several players.
For example, if player1 spawns in, then trigger some code outside the CharacterAdded() then player2 spawns. The code would then use player2 instead of player1.
I think it often makes sense to place code within CharacterAdded(), so hopefully thats a solution that works for you.
This seems to work, thanks! It’s always the most easy solutions which I never think of that work.
@ifkpop I have noted your solution(s) - also good to find out that code within CharacterAdded() is actually sensible - however I don’t need to deal with several players so bikereh’s solution works well for me. Thanks for the help though!
What about within a local script, using LocalPlayer.CharacterAdded:Wait()? Is that inefficient? (Asking because if it is, I have some old scripts to update)