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

How to make parts in the camera follow the player?

I am trying to make a sort of 'fog' in my game by using an inverted sphere and having it attached to the players camera so i don't have to use any physics.

The problem I am running into is that the parts after they are made no longer update their position, so they are just in one spot. How do i make it so they are always attached to the player's position?

Here is the code:

Player = game.Players.LocalPlayer
Character = Player.Character or Player.CharacterAdded:wait()
wait() hrp = Character:FindFirstChild("HumanoidRootPart")

--creating the fog
fogholder = Instance.new("Part", game.Workspace.CurrentCamera)
fog = Instance.new("SpecialMesh", fogholder)

fogholder.Anchored = true
fogholder.CanCollide = false
fogholder.Size = Vector3.new(0,0,0)
fogholder.BrickColor = BrickColor.new(255,255,255)
fogholder.CFrame = CFrame.new(hrp.Position.X, hrp.Position.Y, hrp.Position.Z)

fog.MeshId = "http://www.roblox.com/asset/?id=1185246"
fog.MeshType = "FileMesh"
fog.Scale = Vector3.new(-250,-250,-250)

Also, I don't want the fog to rotate at all.

1 answer

✓ Accepted answer

This one would be quite simple, just use a loop! For example, you could do...

--...
while hrp.Parent do --stop once character (or just HRP) despawns
	--update position
	fogholder.CFrame = CFrame.new(hrp.Position.X, hrp.Position.Y, hrp.Position.Z)
		--wait for next 2 frames (default)
	wait() 
end

Although it's by no means the only way to do it, you could bind it to RenderStepped or the like, go wild.

--EDIT: Here's how you could do just that, although it involves a tad more lines.

--NOTE: Untested.
local RS=game:GetService"RunService" --get RunService
local updateFog=function()
	if not hrp.Parent then --Char is gone! OH NO!
		RS:UnbindFromRenderStep("UpdateFog")
		return
	end
	fogholder.CFrame = CFrame.new(hrp.Position.X, hrp.Position.Y, hrp.Position.Z)
end
--Call the function right before the camera updates, for example
RS:BindToRenderStep("UpdateFog", Enum.RenderPriority.Camera.Value - 1, updateFog)

See these articles for a little more reference.

https://developer.roblox.com/en-us/api-reference/function/RunService/BindToRenderStep

https://developer.roblox.com/en-us/api-reference/function/RunService/UnbindFromRenderStep

4 comments

1

Thanks! this worked, not sure why but loops completely slipped my mind. Just for future reference, as i am quite new to scripting, how would i bind it to RenderStepped?

0

Dun, check edit.

1

Thanks! Ill have to look into that stuff more. It runs much smoother compared to the while loop.

0

Yep, RenderStepped runs each frame the client renders, while wait() waits for a minimum of .03 seconds, which is roughly two frames.

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