How do i make this server sided? (help!)

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)
External Media

thats an example video of local to server

2 Likes

i cant find anything that could help does anyone know???

2 Likes

im not sure i understand do you want to see my tool function script???

2 Likes

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)

2 Likes

your fine. now would i relocate this from local and move it into server script service??

1 Like

place this script in starterplayerscripts

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)

2 Likes

yes. move that script into serverscriptservice

2 Likes

this is the output

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 :arrow_forward: stopRunning called (x2) - Client - StaminaScript:111
16:04:09.159 Successfully disabled the reset button! - Client - DisableReset:12
16:04:10.197 :arrow_forward: 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 :arrow_forward: stopRunning called (x2) - Client - StaminaScript:111

my bad did i forget the remote events manually??

1 Like

you properly placed both scripts i sent? if its not still not working you can send me the file and i can see whats going on

2 Likes

yes the server script FlashlightHandPosition in server script service and the local script FlashlightHandling in startercharacterscripts

can i pm you the studio file??

1 Like

put the other script in starterplayerscripts. and yeah you can pm it to me

1 Like

theres also a more efficient way to do this instead. ill update the scripts in the game file

1 Like

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.

1 Like

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.

1 Like

i tried that but it ended up just rendering the hand in totally different positions every time to the server pov

Did you try directly setting the offset or using motor6ds?

1 Like

yeah it was pretty unreliable or i just did it wrong ill see if i can get the code back because i deleted it upon finding it not working

would you like to see the code?

Sure!!

silly character limit!! haha!!

1 Like