What is the best way to wait for a character in a script that is not a local script?

I changed something about a script which somehow caused another script to error. I know the script is having an error because the script is running before the player’s character is loaded. The issue is that the script is not a local script and it is in the workspace. I have tried to find the answer but it just tells me to use .CharacterAdded:Wait() and whenever I use it, the character is never put into the workspace. Here is the script:

local debounce = false
local inf = true

game.Players.PlayerAdded:connect(function(player)
	task.wait()
	if player then
		repeat
			task.wait(1)
			debounce = true
			if player.Character.Humanoid.Health < .05 and player.Character.Value.Value == 0 then
				player.Character.Humanoid.MaxHealth = 0.1
				player.Character.Value.Value = 1
				task.wait(0.1)
				player.Character.Humanoid.Health = 0.1
			else
				if player.Character.Humanoid.Health <= 0 and player.Character.Value.Value == 1 then
					player.Character.Humanoid.MaxHealth = 0.1
					player.Character.Value.Value = 2
				end
			end
			debounce = false
		until inf == false
	end
end)

You should adjust your code to this:

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        -- Rest of your code
    end)
end)

This means your code will not run until a character is added for that player!

  • .CharacterAdded:Wait() should have done the same thing. Perhaps you implemented it incorrectly.

Also!

  • Remove if player then and task.wait(). The player will always exist if this connection receives a player ingame. task.wait() also does nothing but delay your script 1 Frame (.01677 seconds).

While the script no longer errors, the character is never put into the workspace for some reason.

If the character isn’t put into the workspace then your game is simply unplayable

  • Do you have other scripts running ingame?
  • Try this code in a new baseplate.

I changed a script which caused this to start giving an error. Here is the script I changed:

local Character = script.Parent
local Value = Character.Value
local Player = game.Players:GetPlayerFromCharacter(Character)
local Animations = Character:FindFirstChild("Animate")
table.insert(_G.AlivePlyrs, Character)
print(_G.AlivePlyrs)

local Outline = Instance.new("Highlight")
Outline.Parent = Character
Outline.FillTransparency = 1
Outline.OutlineColor = Color3.new(1,0,0)
Outline.OutlineTransparency = 1

Value:GetPropertyChangedSignal("Value"):Connect(function()
	if Value.Value == 1 then
		Outline.OutlineTransparency = 0
		local pos = table.find(_G.AlivePlyrs, Character)
		table.remove(_G.AlivePlyrs, pos)
		print(_G.AlivePlyrs)
		Animations.idle.Animation1.AnimationId = "https://www.roblox.com/asset/?id=119027085796995"
		Player.CameraMaxZoomDistance = 20
		Character.Humanoid.WalkSpeed = 4
	else
		if Value.Value == 0 then
			Outline.OutlineTransparency = 1
			table.insert(_G.AlivePlyrs, Character)
			print(_G.AlivePlyrs)
			Player.CameraMaxZoomDistance = 0.5
			Character.Humanoid.WalkSpeed = 16
		end
	end
end)

And what I changed was removing the

print(_G.AlivePlyrs)

and added

_G.SpecPlayers

But even after I reverted those, the script still errors.

  • Try this code in a new baseplate.

I don’t know what this code is supposed to do, it looks like it shouldn’t have any effect on this code but you should try disabling this.

If you error is your game totally breaking, you should make a seperate post for that

I figured out that I had accidentally turned off CharacterAutoLoads in Players without realizing it. I’m sorry for the waste of time but thanks for helping.

No problem. let me know if you need more help