Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

CharacterAdded only firing when the character 'respawns' instead of 'spawning'?

The issue is, when the player's character spawns (when the player first joins the game), the code I wrapped inside of CharacterAdded won't run at all. But when my player's character respawns, everything runs just fine.

I don't think it's because the player joined before the server initializes, because I used one of my alt accounts on a different device to join an already opened server, and the results are the same.

I also don't think it's because of the code inside of CharacterAdded, I even tried

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		print("works")
	end)
end)

and it still doesn't print 'works'.

If anyone thinks of what the issue is, please help me out.

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local head = char:WaitForChild("Head")
		local hum = char:WaitForChild("Humanoid")
		local BW = hum:WaitForChild("BodyWidthScale")
		local BH = hum:WaitForChild("BodyHeightScale")
		local BD = hum:WaitForChild("BodyDepthScale")
		local tor = char:WaitForChild("UpperTorso")
		local lhand = char:WaitForChild("LeftHand")
		local rhand = char:WaitForChild("RightHand")
		local lhandswing = Instance.new("Sound", lhand)
		lhandswing.SoundId = "rbxassetid://138097048"
		lhandswing.Name = "LeftHandSwing"
		lhandswing.Volume = 2
		local rhandswing = Instance.new("Sound", rhand)
		rhandswing.SoundId = "rbxassetid://138097048"
		rhandswing.Name = "RightHandSwing"
		rhandswing.Volume = 2
		
		hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
		hum.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
		
		hum.MaxHealth = hp.Value
		hum.Health = hp.Value
		
		hp.Changed:Connect(function()
			hum.MaxHealth = hp.Value
			hum.Health = hp.Value
		end)
		
		local randomBW = math.random(5, 30)
		randomBW = randomBW / 10
		BW.Value = BWSave:GetAsync(k) or randomBW
		BWSave:SetAsync(k, BW.Value)
		
		local randomBH = math.random(5, 30)
		randomBH = randomBH / 10
		BH.Value = BHSave:GetAsync(k) or randomBH
		BHSave:SetAsync(k, BH.Value)
		
		BD.Value = BWSave:GetAsync(k) or randomBW
		
		BW.Changed:Connect(function()
			BD.Value = BW.Value
			BWSave:SetAsync(k, BW.Value)
		end)
		
		BH.Changed:Connect(function()
			BHSave:SetAsync(k, BH.Value)
		end)
		
	end)
end)

1 comment

0

The parent argument to Instance.new() is deprecated. The parent should always be assigned last than first for better performance.

1 answer

Made this comment but decided to make an answer;

'Try waiting for the character's humanoid using an if statement. In solo mode, scripts can load before or after the player loads.'

I've not had any issue with PlayerAdded since before FilteringEnabled became standard but some of the things I usually do is define Services into a variable with GetService. This may be just a micro-optimization but it's something I pretty much do all the time now.


--I read a while back its a good idea doing this but I'm pretty sure it was only a micro optimization
--Its now a habit I do to every service
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
		print("works")
    end)
end)

If it still doesn't work, are you sure this inside a 'Normal' Script.

edit: geez, 4:3 laptops can sure mess with your wording

1 comment

0

Your repeat loop is wrong. It is "repeat wait() until" not repeat then. Also that loop is completely redundant.

This discussion is preserved as it was. New answers, comments, and votes are closed.