i got this script working finally but im unaware as how id make it server sided here i added the comments to make it easier for the functions help is very appreciated
-- LocalScript inside StarterPlayerScripts
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera
-- Function to update the arm position based on cursor direction
local function setupArmTracking(character)
local humanoid = character:WaitForChild("Humanoid")
local rightArm = character:WaitForChild("Right Arm")
local torso = character:WaitForChild("Torso")
local rightShoulder = torso:WaitForChild("Right Shoulder")
local originalC0 = rightShoulder.C0
-- Reference to the flashlight tool
local flashlightTool = player.Backpack:WaitForChild("Flashlight")
local handle = flashlightTool:FindFirstChild("Handle")
local light = handle and handle:FindFirstChildWhichIsA("PointLight") or handle:FindFirstChildWhichIsA("SpotLight")
-- Ensure light and handle are valid before proceeding
if not light or not handle then
warn("Flashlight or light component not found.")
return
end
local lightOn = false
local connection -- Variable to store the RenderStepped connection
local lightOnStartTime = 0 -- Initialize lightOnStartTime
-- Function to update the arm position based on cursor direction
local function updateArm()
if lightOn and flashlightTool.Parent == character and humanoid.WalkSpeed <= 28 and (tick() - lightOnStartTime > 0.5) then
local mousePosition = mouse.Hit.Position
local headPosition = character.Head.Position
local torsoCFrame = torso.CFrame
-- Calculate the direction from the character's head to the mouse position
local direction = (mousePosition - headPosition).unit
-- Convert the direction to the local space of the torso
local relativeDirection = torsoCFrame:VectorToObjectSpace(direction)
-- Calculate the yaw needed to make the arm follow the cursor, inverted by 180 degrees
local yaw = math.atan2(relativeDirection.X, relativeDirection.Z) + math.pi
-- Create the target CFrame based on calculated yaw, only affecting yaw (left/right rotation)
local newC0 = originalC0 * CFrame.Angles(0, yaw, 0)
rightShoulder.C0 = newC0
else
rightShoulder.C0 = originalC0
end
end
-- Function to toggle the light
local function toggleLight()
lightOn = not lightOn
light.Enabled = lightOn
if lightOn then
lightOnStartTime = tick() -- Set the start time when the light is turned on
end
end
-- Function to handle flashlight tool equipped
local function onEquipped()
lightOn = light and light.Enabled
connection = runService.RenderStepped:Connect(updateArm)
end
-- Function to handle flashlight tool unequipped
local function onUnequipped()
if connection then
connection:Disconnect()
connection = nil
end
rightShoulder.C0 = originalC0
end
-- Connect the functions to the flashlight tool's events
flashlightTool.Equipped:Connect(onEquipped)
flashlightTool.Unequipped:Connect(onUnequipped)
flashlightTool.Activated:Connect(toggleLight)
end
-- Initial setup for the current character
if player.Character then
setupArmTracking(player.Character)
end
-- Setup whenever the character respawns
player.CharacterAdded:Connect(setupArmTracking)
my bad it didnt put the whole thing and kept glitching out.
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local mousePositionEvent = replicatedStorage:WaitForChild("MousePositionEvent")
-- Function to update the arm position based on cursor direction
local function setupArmTracking(character)
local humanoid = character:WaitForChild("Humanoid")
local rightArm = character:WaitForChild("Right Arm")
local torso = character:WaitForChild("Torso")
local rightShoulder = torso:WaitForChild("Right Shoulder")
local originalC0 = rightShoulder.C0
local player = players:GetPlayerFromCharacter(character)
-- Reference to the flashlight tool
local flashlightTool = player.Backpack:WaitForChild("Flashlight")
local handle = flashlightTool:FindFirstChild("Handle")
local light = handle and handle:FindFirstChildWhichIsA("PointLight") or handle:FindFirstChildWhichIsA("SpotLight")
-- Ensure light and handle are valid before proceeding
if not light or not handle then
warn("Flashlight or light component not found.")
return
end
local lightOn = false
local connection -- Variable to store the RenderStepped connection
local lightOnStartTime = 0 -- Initialize lightOnStartTime
local mousePosition = Vector3.new() -- Store the mouse position
-- Update mouse position when the client sends it
mousePositionEvent.OnServerEvent:Connect(function(sender, newMousePosition)
if sender == player then
mousePosition = newMousePosition
end
end)
-- Function to update the arm position based on cursor direction
local function updateArm()
if lightOn and flashlightTool.Parent == character and humanoid.WalkSpeed <= 28 and (tick() - lightOnStartTime > 0.5) then
local headPosition = character.Head.Position
local torsoCFrame = torso.CFrame
-- Calculate the direction from the character's head to the mouse position
local direction = (mousePosition - headPosition).unit
-- Convert the direction to the local space of the torso
local relativeDirection = torsoCFrame:VectorToObjectSpace(direction)
-- Calculate the yaw needed to make the arm follow the cursor, inverted by 180 degrees
local yaw = math.atan2(relativeDirection.X, relativeDirection.Z) + math.pi
-- Create the target CFrame based on calculated yaw, only affecting yaw (left/right rotation)
local newC0 = originalC0 * CFrame.Angles(0, yaw, 0)
rightShoulder.C0 = newC0
else
rightShoulder.C0 = originalC0
end
end
-- Function to toggle the light
local function toggleLight()
lightOn = not lightOn
light.Enabled = lightOn
if lightOn then
lightOnStartTime = tick() -- Set the start time when the light is turned on
end
end
-- Function to handle flashlight tool equipped
local function onEquipped()
lightOn = light and light.Enabled
connection = runService.RenderStepped:Connect(updateArm)
end
-- Function to handle flashlight tool unequipped
local function onUnequipped()
if connection then
connection:Disconnect()
connection = nil
end
rightShoulder.C0 = originalC0
end
-- Connect the functions to the flashlight tool's events
flashlightTool.Equipped:Connect(onEquipped)
flashlightTool.Unequipped:Connect(onUnequipped)
flashlightTool.Activated:Connect(toggleLight)
end
-- Initial setup for the current character
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
setupArmTracking(character)
end)
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local replicatedStorage = game:GetService("ReplicatedStorage")
local mousePositionEvent = replicatedStorage:WaitForChild("MousePositionEvent")
-- Send mouse position to the server
mouse.Move:Connect(function()
mousePositionEvent:FireServer(mouse.Hit.Position)
end)
16:04:08.275 Horror survival auto-recovery file was created - Studio
16:04:08.654 A player joined! - Server - PlayerJoin:6
16:04:09.158 stopRunning called (x2) - Client - StaminaScript:111
16:04:09.159 Successfully disabled the reset button! - Client - DisableReset:12
16:04:10.197 stopRunning called (x2) - Client - StaminaScript:111
16:04:10.299 Troublemaiker equipped flashlight - Client - LightToggle:65
16:04:13.081 stopRunning called - Client - StaminaScript:111
16:04:13.548 Infinite yield possible on âReplicatedStorage:WaitForChild(âMousePositionEventâ)â - Studio
16:04:13.548 Stack Begin - Studio
16:04:13.548 Script âServerScriptService.FlashlightHandPositionâ, Line 4 - Studio - FlashlightHandPosition:4
16:04:13.548 Stack End - Studio
16:04:13.701 Infinite yield possible on âReplicatedStorage:WaitForChild(âMousePositionEventâ)â - Studio
16:04:13.701 Stack Begin - Studio
16:04:13.701 Script âPlayers.Troublemaiker.PlayerScripts.FlashlightHandlingâ, Line 4 - Studio - FlashlightHandling:4
16:04:13.701 Stack End - Studio
16:04:13.981 stopRunning called (x2) - Client - StaminaScript:111
i sent it and yeah i probably figured im just pretty new to this i got it working perfectly for local client i just struggle with making server sided stuff
Iâm pretty sure you can still do all of this on the client side. Character replication should cover the arm and flashlight, but turning the flashlight on and off should be serverside.
It might not be the best idea to send a message to the server every time the player moves their mouse since that can get resource intensive. We should try to see if robloxâs character replication can do it instead.