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

How do I make a part within a model turn towards another part?

So here is the background information, I've got a Part called Hoverhealth which is placed within the StarterCharacter. The Part is also attached to the HumanoidRootPart with a motor so I don't have to have a script always have the Part moved over the players head. The Part is also inside the StarterCharacter model so I can relate to the players health and name with ease.

Anyways, my problem is that I need to have the Object rotate and face towards players but I can't use CFrame because that will turn the entire model.

I've got a script that rotates the Hoverhealth part but it also rotates my Character because it uses CFrame, any idea on how to prevent that or to avoid using CFrame?

local part = script.Parent:WaitForChild("Hoverhealth")

local lookAt = game.Workspace.HobGoblin.Torso.Position

while true do
	part.CFrame = CFrame.new(part.Position, Vector3.new(part.Position.X, lookAt.Y, part.Position.Z));
	wait()
end

Thank you

3 comments

0

I don't know why I can't use the Code block thing in this, It wont surround my actual code. Instead it surrounds either everything or just my text and not my code.

0

Just paste in your code, highlight it, and then hit the codeblock button.

0

Fixed the text block problem, apparently you cant tab the beginning of a paragraph

1 answer

You can't just make the health rotate to the player because as there is more than one player in game it wouldnt know what player to rotate to.

what you could make is create a local script that creates a health bar block for every character in game and add bind its movement to renderstep. As it was created by a local part it wont be seen by others, meaning there wont be multiple bars floating around your head.

local bar = Instance.new("Part")--My healthbar model
bar.Anchored = true
bar.CanCollide = false
bar.Size = Vector3.new(3,1,1)
bar.BrickColor = BrickColor.new("Bright red")
local barfill = bar:Clone()
barfill.Name = "Fill"
barfill.Parent = bar
barfill.BrickColor = BrickColor.new("Bright green")
local owner = Instance.new("ObjectValue")
owner.Name = "Owner"
owner.Parent = bar

local bars = {}---------------------------------List of healthbars
local runservice = game:GetService("RunService")
local cam = workspace.CurrentCamera

runservice.RenderStepped:Connect(function(step)--This event fires every frame rendered ingame, so it gets smooth
	for i=1,#bars do
		if bars[i] then--checks if bar still exists
			if bars[i]:findFirstChild("Owner") and bars[i].Owner.Value ~= nil then--checks if the have the owner object value and if it has a non nil value
				bars[i].Parent = workspace
				local offset = Vector3.new(0,-5,0)--This offset is just to show its a 3D object
				bars[i].CFrame = CFrame.new(bars[i].Owner.Value.HumanoidRootPart.Position+Vector3.new(0,5,0),cam.CFrame.Position+offset)
				local hppercentage = bars[i].Owner.Value.Humanoid.Health/bars[i].Owner.Value.Humanoid.MaxHealth--MonsterHP/MonsterMaxHP=Health%
				bars[i].Fill.Size = Vector3.new(hppercentage*bars[i].Size.X,bars[i].Size.Y*1.1,bars[i].Size.Z*1.1)--health%/barsize=current hp bar size
				bars[i].Fill.CFrame = bars[i].CFrame*CFrame.new((bars[i].Size.X-bars[i].Fill.Size.X)/2,0,0)--adjust the position of the current hp bar
			else bars[i]:Destroy()--if it doesnt have an onwer, than destroy this healthbar
			end
		end
	end
end)
function createHealthBar(char)--Clone a healthbar to be used by the char and add it to the healthbars table
	local hp = bar:Clone()
	hp.Owner.Value = char
	table.insert(bars,1,hp)
end
function playerEntered(player)--When Player enters fire the clone healthbar event and connect it with characteradded
	if player.Character then
		createHealthBar(player.Character)
		player.CharacterAdded:Connect(createHealthBar)
	end
end
game.Players.PlayerAdded:Connect(playerEntered)
local s = game.Players:GetChildren()
for i=1,#s do
	playerEntered(s[i])
end

1 comment

0

you can use the createHealthBar function on your mobs. For example: createHealthBar(game.Workspace.HobGoblin)

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