player.CharacterAdded:Wait() not working on first join

Hello! I have a problem with my code that ive tried to solve, but i cant seem to fix it no matter what!
Whenever a player joins a game, its supposed to wait until the players character is added, to ensure the player.Character:WaitForChild(“Torso”).Touched:Connect(OnTouch) will work, without a attempt to index nil. But instead, it wont. I tried debugging it by adding a print, and it doesnt seem to fire, until the player resets!

Any help is appreciated!

local player = game.Players.LocalPlayer

player.CharacterAdded:Wait()
player.CharacterAppearanceLoaded:Wait()
print("Loaded Local Script")

local function OnTouch(hit)
	if hit.Name == "DoorHitbox" then
		if hit:WaitForChild("Cooldown").Value == true then
			
		else
			hit:WaitForChild("Cooldown").Value = true
			local rand = math.random(1, 102)
			local choose = math.random(1, 2)
			player.Character:WaitForChild("Humanoid").JumpPower = 0
			player.Character:WaitForChild("Humanoid"):MoveTo(hit.CFrame)
			player.Character:WaitForChild("Humanoid").MoveToFinished:Wait()
			if choose == 1 then
				script.DoorBell:Play()
			elseif choose == 2 then
				script.Doorknock:Play()
			end
			player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			task.wait(3)
			script.Open:Play()
			hit.Parent:WaitForChild("Door").Transparency = 1
			local waitcooldown = coroutine.create(function()
				task.wait(math.random(30, 60))
				hit:WaitForChild("Cooldown").Value = false
				for i, v in pairs(hit.Parent:GetChildren()) do
					if v.Name == "Light" then
						v.Color = Color3.fromRGB(255, 255, 0)
					end
				end
			end)
			task.wait(3)
			if rand <= 75 then
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Treat")
				print("Treat!")
			elseif rand <=75+25 then
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Trick")
				print("TRICKED YOU FOOL!!!!")
			elseif rand <75+25+1 then
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Jackpot")
				print("JACKPOT!!!!")
			else
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Death")
				print("Uh oh.")
			end
			for i, v in pairs(hit.Parent:GetChildren()) do
				if v.Name == "Light" then
					v.Color = Color3.fromRGB(0, 0, 0)
				end
			end
			coroutine.resume(waitcooldown)
			script.Close:Play()
			player.Character:WaitForChild("Humanoid").WalkSpeed = 16
			player.Character:WaitForChild("Humanoid").JumpPower = 50
			hit.Parent:WaitForChild("Door").Transparency = 0
		end
	end
end

player.Character:WaitForChild("Torso").Touched:Connect(OnTouch)

Maybe try this:

local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
print("Loaded Local Script")

local function OnTouch(hit)
	if hit.Name == "DoorHitbox" then
		if hit:WaitForChild("Cooldown").Value == true then
			
		else
			hit:WaitForChild("Cooldown").Value = true
			local rand = math.random(1, 102)
			local choose = math.random(1, 2)
			player.Character:WaitForChild("Humanoid").JumpPower = 0
			player.Character:WaitForChild("Humanoid"):MoveTo(hit.CFrame)
			player.Character:WaitForChild("Humanoid").MoveToFinished:Wait()
			if choose == 1 then
				script.DoorBell:Play()
			elseif choose == 2 then
				script.Doorknock:Play()
			end
			player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			task.wait(3)
			script.Open:Play()
			hit.Parent:WaitForChild("Door").Transparency = 1
			local waitcooldown = coroutine.create(function()
				task.wait(math.random(30, 60))
				hit:WaitForChild("Cooldown").Value = false
				for i, v in pairs(hit.Parent:GetChildren()) do
					if v.Name == "Light" then
						v.Color = Color3.fromRGB(255, 255, 0)
					end
				end
			end)
			task.wait(3)
			if rand <= 75 then
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Treat")
				print("Treat!")
			elseif rand <=75+25 then
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Trick")
				print("TRICKED YOU FOOL!!!!")
			elseif rand <75+25+1 then
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Jackpot")
				print("JACKPOT!!!!")
			else
				game.ReplicatedStorage:WaitForChild("ReceiveRemote"):FireServer("Death")
				print("Uh oh.")
			end
			for i, v in pairs(hit.Parent:GetChildren()) do
				if v.Name == "Light" then
					v.Color = Color3.fromRGB(0, 0, 0)
				end
			end
			coroutine.resume(waitcooldown)
			script.Close:Play()
			player.Character:WaitForChild("Humanoid").WalkSpeed = 16
			player.Character:WaitForChild("Humanoid").JumpPower = 50
			hit.Parent:WaitForChild("Door").Transparency = 0
		end
	end
end

character:WaitForChild("Torso").Touched:Connect(OnTouch)

Didnt seem to have fixed it… still not going through, i dont see the print, nor does the script work.

Try removing this part:

player.CharacterAppearanceLoaded:Wait()
1 Like