I’m working on a Roblox game where players leave a “fallen body” with a floating username above it. When the player rejoins, I want their camera to smoothly follow the floating username animation (CFrames stored in a Frames folder, with settings in a Settings folder).
I already have: The username clone with a Frames folder (all CFrameValues) and Settings folder with Loop and Reference values.
I want:
- The client camera to follow the username along the CFrames when they rejoin.
- The animation to optionally loop if
Loopis true.
How can I properly bind the camera to follow the username clone using RunService or another method? Any example or guidance would help!
local BodyManager = {}
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("Player_Bodies")
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local TeleportService = game:GetService("TeleportService")
local ServerStorage = game:GetService("ServerStorage")
local Character_Reserves = ServerStorage.CharacterReserves
local PowerOn_Anim = "rbxassetid://90885075696141"
local PowerOff_Anim = "rbxassetid://122053069775967"
local Idle_Anim = "rbxassetid://115055774181265"
-- Save player data
local function SaveData(Player)
local Data = {
JobId = game.JobId,
TimeStamp = os.time(),
}
local Save, err = pcall(function()
DataStore:SetAsync(Player.UserId, Data)
end)
if not Save then
warn(err)
else
print(Player.Name.."'s character has been reserved!")
end
end
-- Remove saved player data
local function RemoveData(UserId)
local Removed, err = pcall(function()
DataStore:RemoveAsync(UserId)
end)
if not Removed then
warn(err)
else
print(UserId.." character has been destroyed!")
return true
end
end
-- Get saved data
local function GetData(UserId)
for i = 1, 3 do
local success, Data = pcall(function()
return DataStore:GetAsync(UserId)
end)
if success and Data then
return Data
end
task.wait(1)
end
return nil
end
-- Play PowerOn animation
local function PlayAnim(Humanoid,Id)
local Animator = Humanoid:FindFirstChildOfClass("Animator")
if not Animator then
return
end
if Id == "" then
return
end
local Anim = Instance.new("Animation")
Anim.AnimationId = Id
local track = Animator:LoadAnimation(Anim)
track:Play()
return track
end
-- Update the reserve in storage
local function UpdateReserve(Character)
if not Character then
return
end
if Character_Reserves:FindFirstChild(Character.Name) then
Character_Reserves:FindFirstChild(Character.Name):Destroy()
end
local Clone = Character:Clone()
Character.Parent = Character_Reserves
end
-- When player leaves, create body and make it fallen
local function onPlayerRemoving(Player)
if Player:GetAttribute("Teleporting") then
return
end
local Spirit = script.Spirit:Clone()
local Clone = Character_Reserves:FindFirstChild(Player.Name)
if not Clone then
return
end
Clone:SetAttribute("OwnerId", Player.UserId)
local Head = Clone:WaitForChild("Head")
-- Set PlatformStand so the body stays fallen
local Humanoid = Clone:FindFirstChildOfClass("Humanoid")
PlayAnim(Clone:WaitForChild("Humanoid"),PowerOff_Anim)
task.delay(3,function()
PlayAnim(Clone:WaitForChild("Humanoid"),Idle_Anim)
end)
if Humanoid then
Humanoid.PlatformStand = true
Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
end
-- Spirit/name effect
Spirit.Parent = workspace
Spirit.BillboardGui.PlayerName.Text = Player.Name
Spirit.Position = Vector3.new(Head.Position.X, Head.Position.Y, Head.Position.Z)
if Head:FindFirstChildOfClass("BillboardGui") then
Head:FindFirstChildOfClass("BillboardGui"):Destroy()
end
Clone.Parent = workspace
task.spawn(function()
for i=1,100 do
Spirit.Position += Vector3.new(0,1,0)
task.wait(.015)
end
for i=1,100 do
Spirit.Position += Vector3.new(0,0.7,0)
task.wait(.008)
end
Spirit:Destroy()
end)
SaveData(Player)
end
-- Track alive servers
local AliveServers = {}
local function isJobIdAlive(targetJobId)
local lastPing = AliveServers[targetJobId]
return lastPing and os.time() - lastPing <= 20
end
-- Camera follow helper
local function StartCameraFollow(player, hrp)
local cam = workspace.CurrentCamera
local animFolder = script:WaitForChild("CameraFollowAnim") -- folder containing Frames & Settings
local framesFolder = animFolder:WaitForChild("Frames")
local settings = animFolder:WaitForChild("Settings")
local loop = settings:WaitForChild("Loop").Value
local reference = settings:WaitForChild("Reference")
local hasReference = reference:FindFirstChild("Has") and reference.Has.Value
if hasReference then
task.spawn(function()
repeat
for _, frame in ipairs(framesFolder:GetChildren()) do
if frame:IsA("CFrameValue") then
cam.CFrame = frame.Value + hrp.Position -- adjust offset if needed
task.wait() -- tweak speed as needed
end
end
until not loop
end)
end
end
-- When player joins
local function onPlayerAdded(Player)
local Spirit = script.Spirit
Player.CharacterRemoving:Connect(UpdateReserve)
local function CharacterAdded(Character)
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
if Player then
Player:LoadCharacter()
end
end)
-- Add floating name above head
if not Character:WaitForChild("Head"):FindFirstChildOfClass("BillboardGui") and not Character:FindFirstChild("Spirit") then
local Clone = Spirit.BillboardGui:Clone()
Clone.PlayerName.Text = Player.Name
Clone.Parent = Character:WaitForChild("Head")
end
end
local Data = GetData(Player.UserId)
if Data then
if Data.JobId == game.JobId then
local OldBody = workspace:FindFirstChild(Player.Name)
if not OldBody then
for _,v in pairs(workspace:GetChildren()) do
if v:GetAttribute("OwnerId") == Player.UserId then
OldBody = v
end
end
end
if OldBody then
Player.Character = OldBody
local Humanoid = OldBody:FindFirstChildOfClass("Humanoid")
if Humanoid then
Humanoid.PlatformStand = false
Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
end
local Head = OldBody.Head
local Clone = Spirit:Clone()
Clone.BillboardGui.PlayerName.Text = Player.Name
local HeadPos = Head.Position
Clone.Position = Vector3.new(HeadPos.X, HeadPos.Y+30, HeadPos.Z)
Clone.Parent = Player.Character
task.spawn(function()
for i=1,100 do
Clone.Position -= Vector3.new(0,.2,0)
task.wait(.01)
end
for i=1,100 do
Clone.Position -= Vector3.new(0,.1,0)
task.wait(.01)
end
if not Player.Character:WaitForChild("Head"):FindFirstChildOfClass("BillboardGui") then
Clone.BillboardGui.Parent = Head
Clone:Destroy()
end
for _,v in pairs(OldBody:FindFirstChildWhichIsA("Humanoid"):GetPlayingAnimationTracks()) do
if v.IsPlaying then
v:Stop()
end
end
-- Play PowerOn, then respawn at same pos/rot
local track = PlayAnim(Player.Character:WaitForChild("Humanoid"),PowerOn_Anim)
if track then
track.Stopped:Wait()
if Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
local hrp = Player.Character.HumanoidRootPart
local cf = hrp.CFrame
Player:LoadCharacter()
local newChar = Player.Character or Player.CharacterAdded:Wait()
local newHrp = newChar:WaitForChild("HumanoidRootPart")
newHrp.CFrame = cf -- restore pos+rotation
-- Start camera follow
StartCameraFollow(Player, newHrp)
end
end
end)
else
RemoveData(Player.UserId)
Player:LoadCharacter()
end
else
Player:SetAttribute("Teleporting", true)
local Teleported = false
if isJobIdAlive(Data) then
for i = 1, 3 do
print("Attempting to teleport "..Player.Name)
local attempt, err = pcall(function()
TeleportService:TeleportToPlaceInstance(game.PlaceId, Data.JobId, Player)
end)
if attempt then
print("Teleporting "..Player.Name)
Teleported = true
break
else
task.wait(1)
end
end
end
if not Teleported then
print("Failed to teleport "..Player.Name..", server might've shutdown!")
Player:SetAttribute("Teleporting", nil)
RemoveData(Player.UserId)
Player:LoadCharacter()
end
end
else
Player:LoadCharacter()
local Clone = Spirit.BillboardGui:Clone()
Clone.PlayerName.Text = Player.Name
Clone.Parent = Player.Character:WaitForChild("Head")
end
CharacterAdded(Player.Character)
Player.CharacterAdded:Connect(CharacterAdded)
end
-- Check for bodies falling into void
local function CheckForBodies()
for _,voidPart in pairs(workspace.Void:GetChildren()) do
voidPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
local Character = hit.Parent
if Character:GetAttribute("OwnerId") then
local Success = RemoveData(Character:GetAttribute("OwnerId"))
if Success then
Character:SetAttribute("OwnerId",nil)
end
elseif Players:FindFirstChild(Character.Name) then
Players:FindFirstChild(Character.Name):LoadCharacter()
end
end
end)
end
end
-- Messaging service for alive servers
local function SubscribeToJob(targetJobId)
pcall(function()
MessagingService:SubscribeAsync("ServerAlive_" .. targetJobId, function(message)
AliveServers[targetJobId] = message.Data
end)
end)
end
SubscribeToJob(game.JobId)
-- Keep server alive
local function ActiveServer()
task.spawn(function()
while task.wait(20) do
pcall(function()
MessagingService:PublishAsync("ServerAlive_" .. game.JobId, os.time())
end)
end
end)
end
-- Initialize BodyManager
function BodyManager.Init()
Players.CharacterAutoLoads = false
Players.PlayerRemoving:Connect(onPlayerRemoving)
Players.PlayerAdded:Connect(onPlayerAdded)
CheckForBodies()
end
return BodyManager