How to make a tool server side

Hi everyone,

I want to make a flashlight tool, but unfortunately the light only shows up on the client’s side.

Here’s a LocalScript inside the tool (it works fine)

local cam = workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Clone = RS:WaitForChild("LightPart"):Clone()
Clone.Parent = script.Parent

local Brightness = 10

local Keybind = Enum.KeyCode.F

local UIS = game:GetService("UserInputService")

local Toggle = false

local Mouse = game.Players.LocalPlayer:GetMouse()

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)


UIS.InputBegan:Connect(function(Input, p)
	if script.Parent.Equipped then
		if p then return end
		if Input.KeyCode == Keybind then
			Toggle = not Toggle
		end
	else
		return
	end
end)

script.Parent.Unequipped:Connect(function()
	Toggle = false
end)

RunService.RenderStepped:Connect(function()
	if Clone then

		Clone.Position = cam.CFrame.Position
		TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, Mouse.Hit.Position)}):Play()

		if Toggle then
			TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()

		else
			TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
		end

	end
end)

The LocalScript works well on the client’s side, but I want all the players to see the light move. Is there any way to “convert” this script to server-side so it shows up for all players?

Thanks for any help!

You should be able to make it so that you just require to fire a remote event and then add the code that creates the light inside of a function connected to the event that runs when the remote event is fired.

The short answer is you can’t make a LocalScript execute on the server.
The problem is that your LocalScript is dependent on LocalPlayer.GetMouse() which is not a server-side property.
You will need to move your code to a Script that is parented to the server, or to a Script inside of a remote event.
Server-Side Script
You will need to separate your code into two parts.

The part that needs to be client-side, such as the part that changes the tool’s brightness based on the mouse.
The part that needs to be server-side, such as the part that updates the position of the light part.

You can then put the server-side code into a Script inside of ReplicatedStorage and call it from the client-side code.
Remote Event Script
You can also put your entire script into a Script inside of a RemoteEvent.
When you want to execute the script, the client can “fire” the remote event. The server will then run the script.
This will be more difficult because you will need to pass more arguments to the remote event. However, this method will allow you to keep all of your code in one place.