Setting the PrimaryPart to the HRP automatically with a plugin | Creating a basic plugin

Notes:
HRP = HumanoidRootPart,
This tutorial assumes you have basic knowledge of the lua(U) language.


It is as of recent that I realized that for whatever reason roblox is now setting rigs to have their primary part as the head, now this is fine, but in some cases it can cause problems for animations.


Take for instance you have an animation where you move the torso downwards, if you’re PrimaryPart is set to the Head, well guess what. Nothing happens! Where as if it is set as the HRP you can well actually move the Torso around.


Now this can get kinda annoying when you are doing animations, so I have come with a solution!

For starters, lets make it so when a character is created in studio is PrimaryPart is automatically set to the HRP:


First create a script (Server Script) and parent it to where ever you want!
Second we need two things, we need RunService, and to tell when a descendant is added to the workspace, so:

local runService = game:GetService('RunService') -- // Get RunService //

workspace.DescendantAdded:Connect(function(char) -- // This triggers when a descendant is added to the workspace! //

end)

Now that we have that we need to be able to tell when a character is added, otherwise we could end up doing this to any kind of instance which would probably result in errors!

So:

local runService = game:GetService('RunService')

workspace.DescendantAdded:Connect(function(char)
	if char:IsA('Model') then -- // Check if "char" is a model //
	end
end)

Now that we’ve checked that, lets make sure this is a character to begin with. So how can we do that? Well the simplest way is to just check if there is a humanoid inside of char.
We also need to make sure char even has a HRP to begin with!

local runService = game:GetService('RunService')

workspace.DescendantAdded:Connect(function(char)
	if char:IsA('Model') then
		if not char:FindFirstChildOfClass('Humanoid') then return end -- // Check if the char contains a humanoid, if not we call the return function to stop this from running //
		if not char:FindFirstChild('HumanoidRootPart') then return end -- // Here we check if the char has a HRP, if not we use the return function to stop the code //
	end
end)

Great! Now that we’ve made it so we know that we can set the HRP, lets do it!

local runService = game:GetService('RunService')

workspace.DescendantAdded:Connect(function(char)
	if char:IsA('Model') then
		if not char:FindFirstChildOfClass('Humanoid') then return end
		if not char:FindFirstChild('HumanoidRootPart') then return end
		
		char.PrimaryPart = char:FindFirstChild('HumanoidRootPart')
	end
end)

And its as simple as that. :>

BUT WAIT!! Lets say at some point you have a character and in game you set its PrimaryPart to be the Head, or heck something else, well this plugin is going to mess with it, and set it back the HRP, now this is where RunService comes in. We can use it to detect if the game is in run mode, that way we can stop it from running! To do that all we have to do is add this one line of code:

local runService = game:GetService('RunService')

workspace.DescendantAdded:Connect(function(char)
	if runService:IsRunMode() or runService:IsRunning() then return end -- // Make sure we are NOT in run mode, that way it shouldnt mess with everything else.
	
	if char:IsA('Model') then
		if not char:FindFirstChildOfClass('Humanoid') then return end
		if not char:FindFirstChild('HumanoidRootPart') then return end
		
		char.PrimaryPart = char:FindFirstChild('HumanoidRootPart')
	end
end)

now that we have the code done for the script, we need it to well, actually run. So to do this we need to turn it into a plugin!
To do this, Right Click onto the script, a list of options should come onto screen, near the bottom of the list is the option to “Save as a local Plugin”, Select that option, another menu should appear asking you to save it, or cancel, you can also edit the name of what you want the plugin to be called in your files there. After you’ve selected a name for the file, press save, and now the plugin should work!

Note: Make sure if you edit the name of the file, make sure that you keep the file as a .lua

And thats how its done!
I dont make tutorials often so any feedback on this would be nice, if you have any questions regarding the tutorial or plugins in general, feel free to contact me! I love helping out the community and just making plugins all together so I really dont mind!

If you want more practice with plugins you could always try taking the skills we used here to make something for instance, an auto anchor plugin, that way you dont need to anchor your parts!

Anyhow have a nice day~
~ Frodev

4 Likes