Crie um script em Lua para o Roblox que faça o seguinte: Quando um jogador sentar em um assento com a tag "Head", a cabeça do personagem deve seguir o movimento da câmera de forma fluida e realista, respeitando limites para a rotação horizontal (esquerda/direita) e vertical (cima/baixo). Certifique-se de implementar suavidade nos movimentos para evitar comportamentos bruscos. Além disso, ao sair do assento, a função deve ser interrompida e a cabeça do personagem deve voltar à sua posição padrão. O script deve ser eficiente e bem estruturado
Assistant
To accomplish the task, we need to manipulate the player's character head to follow the camera movement when seated in a "Head" tagged seat, with smooth and realistic motion, respecting rotation limits.
Since the camera is client-side and character manipulation needs to replicate to all clients, we'll use a combination of LocalScript and server Script with RemoteEvent to synchronize the head movement. Here's the complete solution:
1. LocalScript (Place this under StarterPlayer > StarterPlayerScripts):
lua
-- LocalScript under StarterPlayerScripts
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HeadRotationEvent")
local updatingHead = false
local heartbeatConnection = nil
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local function onSeated(active, seat)
if active and seat then
if CollectionService:HasTag(seat, "Head") then
if not updatingHead then
updatingHead = true
heartbeatConnection = RunService.Heartbeat:Connect(function()
if updatingHead then
local camera = workspace.CurrentCamera
local cameraCFrame = camera.CFrame
-- Transform camera's look vector into character's local space
local relativeCFrame = rootPart.CFrame:ToObjectSpace(cameraCFrame)
-- Extract rotation angles
local x, y, z = relativeCFrame:ToEulerAnglesXYZ()
-- Clamp the angles
local maxDownAngle = math.rad(45)
local maxUpAngle = math.rad(45)
local maxLeftRightAngle = math.rad(90)
local clampedX = math.clamp(x, -maxUpAngle, maxDownAngle)
local clampedY = math.clamp(y, -maxLeftRightAngle, maxLeftRightAngle)
-- Send the angles to the server
remoteEvent:FireServer(clampedX, clampedY)
end
end)
end
end
else
if updatingHead then
updatingHead = false
if heartbeatConnection then
heartbeatConnection:Disconnect()
heartbeatConnection = nil
end
remoteEvent:FireServer(nil, nil) -- Reset head rotation
end
end
end
humanoid.Seated:Connect(onSeated)
end
Player.CharacterAdded:Connect(onCharacterAdded)
if Player.Character then
onCharacterAdded(Player.Character)
end
Explanation:
The LocalScript listens for when the player sits in a seat tagged "Head".
When seated, it starts sending the clamped camera rotation angles to the server via RemoteEvent.
Angles are clamped to ensure realistic rotation limits.
The updates are sent during Heartbeat, which aligns with the frame updates, ensuring smooth motion.
2. Server Script (Place this under ServerScriptService):
lua
-- Script in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "HeadRotationEvent"
remoteEvent.Parent = ReplicatedStorage
local updatingPlayers = {}
local lastUpdateTimes = {}
remoteEvent.OnServerEvent:Connect(function(player, x, y)
local now = tick()
local lastUpdate = lastUpdateTimes[player] or 0
if now - lastUpdate < 0.05 then return end -- Limit updates to 20 times per second
lastUpdateTimes[player] = now
local character = player.Character
if not character then return end
local head = character:FindFirstChild("Head")
local neck = head and head:FindFirstChild("Neck")
if not neck then return end
if x and y then
-- Store original C0 if not stored
if not neck:FindFirstChild("OriginalC0") then
local originalC0 = Instance.new("CFrameValue")
originalC0.Name = "OriginalC0"
originalC0.Value = neck.C0
originalC0.Parent = neck
end
local originalC0 = neck.OriginalC0.Value
local desiredC0 = originalC0 * CFrame.Angles(x, 0, 0) * CFrame.Angles(0, y, 0)
neck.C0 = neck.C0:Lerp(desiredC0, 0.3)
updatingPlayers[player] = true
else
-- Reset neck C0
if neck:FindFirstChild("OriginalC0") then
neck.C0 = neck.OriginalC0.Value
neck.OriginalC0:Destroy()
end
updatingPlayers[player] = nil
end
end)
local function onPlayerRemoving(player)
lastUpdateTimes[player] = nil
updatingPlayers[player] = nil
end
Players.PlayerRemoving:Connect(onPlayerRemoving)
Explanation:
The server script creates a RemoteEvent to receive head rotation updates from clients.
It updates the Neck joint's C0 property to rotate the character's head, ensuring replication to all clients.
Uses Lerp to interpolate between the current and desired neck orientations for smooth movement.
Respects rotation limits sent from the client.
Implements update throttling to ensure server efficiency (limits updates to 20 times per second).
Resets the head orientation when the player leaves the seat or stops sending updates.
Important Notes:
Ensure that all seats intended for this functionality have the tag "Head" applied using CollectionService.
The RemoteEvent must be securely handled to prevent exploitation. Since we're only accepting data from the client regarding their own character and clamping rotation values, this reduces risk.
Adjust rotation limits (maxUpAngle, maxDownAngle, maxLeftRightAngle) as needed for your game.
By following this script, players seated in tagged seats will have their character's head smoothly follow their camera movement, with realistic limitations and without abrupt changes, efficiently updating and resetting as they leave the seat.