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

Why does my part go to nowher near my camera?

Hi (again)! I am trying to make a part follow where my camera is (if you don't know yet). It doesn't go anywhere near the camera and goes in one certain spot. Help! (there is no error)

local owner = game.Players.iiDkOffical
local head = Instance.new("Part")
head.Name = "Head"
head.Anchored = true
print("Made Part")
local group = Instance.new("Model")
group.Name = "Unknown"
group.Parent = game:GetService("Workspace")
head.Parent = group
local humanoid = Instance.new("Humanoid")
humanoid.Parent = group
print("Made group")
head.Size = Vector3.new(1,1,1)
while true do
wait(0.25)
group.Name = owner.Name.." - Camera"
head.CFrame = game:GetService("Workspace").Camera.CFrame
end

2 answers

CurrentCamera is the actual Camera that is adjusted on the Player's screen, referencing 'Camera' will be setting the CFrame to a static position. Since the Camera is also rendered every Frame, we can tell the Part to match it's position at the same rate with RunService's .Heartbeat signal. This way we can make it follow the Camera smoothly.

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	head.CFrame = workspace.CurrentCamera.CFrame
end)

6 comments

0

nope, still doesn't follow the camera

0

Ensure this is a LocalScript, as I said before.

0

can you make it a script?

0

You'll still need a LocalScript to have this properly work. Use a RemoteFunction to call upon the Players Camera CFrame through the LocalScript, then the ServerScript to adjust the Part.

0

i knew that would be the answer

0

I tested my code above, conjoined with yours, it works perfectly fine, but I'm not necessarily sure why you want this to happen? It will constantly be interrupting the Camera's FOV causing it to push forward. If you want something to float above the Characters head? Then you can simply weld a part to it.

Just made the script a bit neater, this should work

--- put this in a localscript under StartPlayerScripts which is under StarterPlayer
local camera = workspace.CurrentCamera
local part

wait(3) -- just to make sure everything loaded

game["Run Service"].Heartbeat:Connect(function()
	if not part then
		local player = game.Players.LocalPlayer
		local char = player.Character
		part = Instance.new("Part")
		part.Name = "CAMERAPART"
		part.Anchored = true
		part.Parent = char
		part.CFrame = camera.CFrame
	end
	part.CFrame = camera.CFrame
end)

Hope I helped, if you have any questions please let me know

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