game:GetService("Players").PlayerAdded:Connect(function(plr)
--Stuff here
print("playeradded event ran")
plr.CharacterAdded:Connect(function(character)
--This may be the issue idk
if not character.Parent then repeat task.wait() until character end --detects if the character is inside workspace
print(character) --This doesnt always run
local Humanoid = character:WaitForChild("Humanoid")
--Stuff here
print("characteradded event ran")
Humanoid.Died:Connect(function()
print("humanoid died event ran")
If i recall correctly. I did read somewhere some yrs ago that “CharacterAdded” doesnt sometimes fire because the character can load into the game before the signal has a chance to fire(Correct me if im wrong).
You could try to wait until the ancestry of the character has changed to make sure its not nil.
Modified bit of your code.
(Edit: Saw you already tried this example so idk then. Ill return to this topic later if it hasnt been solved because i dont have that much time on my hands atm.)
plr.CharacterAdded:Connect(function(character)
if character.Parent == nil then
repeat character.AncestryChanged:Wait() until character.Parent ~= nil
end
--This may be the issue idk
print(character) --This doesnt always run
local Humanoid = character:WaitForChild("Humanoid")
--Stuff here
print("characteradded event ran")
Humanoid.Died:Connect(function()
print("humanoid died event ran")
end)
end)
game:GetService("Players").PlayerAdded:Connect(function(plr)
print("playeradded event ran")
plr:LoadCharacter() --Added this
plr.CharacterAdded:Connect(function(character)
if character.Parent == nil then
repeat character.AncestryChanged:Wait() until character.Parent ~= nil
end --detects if the character is inside workspace
print(character)
print(character.Parent)
@Herobrinekd1 might be correct, The Character may be loading in before the CharacterAdded Event is Connected, In order to Prevent this, you may need to ceate your own Character Loader, which would Involve waiting a bit, and then to Load the Character.
But, How do you Stop the Character from Loading in the Game? game.Players has a Property called CharacterAutoLoads, which essentially allows for the character to Auto Repsawn, When we Disable this, the Character will not spawn, and we will now have to do this manually with Player:LoadCharacter, so what I recommend that you do is: Connect all the Events that will be used, This is so when the Load the Character, it will already have these properly set up to use, which would look like this:
Players.PlayerAdded:Connect(function(Player)
-- Connect the 'CharacterAdded' Event
-- Connect any other Event you might use
Player:LoadCharacter() -- Do this at the very end of the Event
end)
But when the Character Dies, they will not Respawn, so you would need the Humanoid.Died Event to wait the amount for the RespawnTime, and then Load the Character
Humanoid.Died:Connect()
task.delay(Players.RespawnTime, function()
Player:LoadCharacter()
end)
-- Delays for the Amount of time in RepsawnTime and Reloads the Character
end)
This may help with CharacterAdded Working Properly.
You dont need to completely exit your game to Save a Script, you can Have it save Automatically either by closing the Script Tab, or when running the game.