CharacterAdded vs StarterCharacter

I’ve always been wondering this, in what cases should I use CharacterAdded in ServerScriptService and when should I use a script in StarterCharacter, which one is better for handling character? and why?

3 Likes

You should use a script in the StarterCharacterScripts when the script is a bit more “personal”, and use it in ServerScriptService when it’s a bit more “global”.
For example, If you want to change the clothing of a character when they join, you should do that in the ServerScriptService, since it has to be done to every character, and also CharacterAdded fires when the character gets loaded so its perfect.
However, if you want to do something like changing the character’s size based on some other values like its position, for example, then you should do that in a script in StarterCharacter, as the script should only run for one single character, and it doesn’t concern any of the other characters.

Hope this helps

4 Likes

CharacterAdded is an event which fires whenever the character of the player spawn or respawns. All models placed inside the StarterCharacter folder will be copied when the player character spawns or respawn. The objects would be loaded as the default character.

For example, if you want to fling the player character everytime they spawn, you should use the CharacterAdded event. Now if you want to change your character without having to use a script, you just need to put a model of the character inside the StarterCharacter and the engine would automatically replace it.

3 Likes

My game uses ForceField when player is near shop and they anchored too, but exploiters can just unanchor then move freely with FF, to combat this I implemented a distance check. But the problem is, it’s in startercharacter and I don’t want a loop for every player in a 30 players server, so I wanted to move it to ServerScriptService, however I wanted to know if it’s gonna be global or not.

task.spawn(function()
	T = Run.Heartbeat:Connect(function(dt)
		pcall(function()
			if Humanoid.Health <= 0 then
				T:Disconnect()
			end
			if (Character.PrimaryPart.Position - _G.CattoShop.Position).Magnitude >= 10 then
				Character:FindFirstChild("CattoShopFF"):Destroy()
			end
			if (Character.PrimaryPart.Position - _G.Dealer.Position).Magnitude >= 10 then
				Character:FindFirstChild("DarkShopFF"):Destroy()
			end
		end)
	end)
end)
2 Likes

Instead of using a distance check, you could just remove their Force Field server sided once they moved.

2 Likes

Like this?

Character:FindFirstChildOfClass("Humanoid").Changed:Connect(function(Property)
	if Property == "MoveDirection" then
		if Character.Humanoid.MoveDirection.Magnitude >= 1 then
			--destroy
		end
	end
end)
2 Likes

If you implement it in a single ServerScript, you would still have to loop thourhg all 30 players from that one script, and make it do what it does 30 times in each frame. If its in the StarterCharacter, it would only do that one thing once in a frame, decreasing the pressure on that single thread.

Also, I don’t recommend using Events like Heartbeat for something like a position check, since its too fast and might cause performance issues. You could use a slower loop like while wait(1/20) do (not the best solution but it can be used) or just simply put a PropertyChangedSignal on the character so it fires when their position has changed.

3 Likes

Yes. Also add a boolean which represents if they’re outside or inside the shop so you can check it before checking if they moved.

3 Likes