Flashlight code not working

Hi everyone,

I’m attempting to create a script where all players can see a light moving when the player moves their mouse. I started with a great script from this video:

However, I noticed that it was on a LocalScript, meaning no one else could see the light moving. Thus, I used a pair of Remote Events to try and connect the original modified script and a normal Script.

Edit: There are no errors in Output. When I equip the light, it doesn’t move.

Here’s the model if you want to see what’s wrong:
Flashlight.rbxm (30.9 KB)
Make sure to put it in StarterPack.

Layout:

image

Local Script

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

local Brightness = 10

local Keybind = Enum.KeyCode.F

local UIS = game:GetService("UserInputService")

local Toggle = false

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

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

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

RunService.RenderStepped:Connect(function()
	if Clone then
		if not Toggle then
			Clone.Position = cam.CFrame.Position
			move:FireServer(Mouse.Hit.Position)
		end
	end
end)

Server Script

local light = script.Parent:WaitForChild("Light")
local move = script.Parent:WaitForChild("Move")

local Clone = script.Parent:WaitForChild("LightPart")

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

local Brightness = 10

move.OnServerEvent:Connect(function(p, pos)
	TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, pos)}):Play()
	print(pos)
end)

light.OnServerEvent:Connect(function(p, bool)
	if bool then
		TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()

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

Can anyone help? Anything is appreciated.

Thank you!

use mouse.move:connect(function)- so the mouse hit postition will change only whene moved

mouse.move:connect(function)
if Clone then
if not Toggle then
Clone.Position = cam.CFrame.Position
move:FireServer(Mouse.Hit.Position)
end
end
end)

I think that’s not the issue, as the light just does not move… If you don’t mind, could you test it with the download link?

From me testing it out. It just seems like the issue is CFrame.lookAt()

look you sent to the server the same location of mouse not the new location

It looks like you are trying to make a light that follows the mouse cursor when a player equips an item.

To achieve this, you can use a Script object inside the item instead of a LocalScript , and have it communicate with the server using RemoteFunction s and RemoteEvent s.

Here is a modified version of your script that should work:

Local Script:

local cam = workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local Keybind = Enum.KeyCode.F
local Toggle = false
local Mouse = game.Players.LocalPlayer:GetMouse()

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

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

RunService.RenderStepped:Connect(function()
	if Toggle then
		RS.Move:InvokeServer(Mouse.Hit.Position)
	end
end)

Server Script:

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

local Brightness = 10

RS.Move.OnServerInvoke = function(player, pos)
	local Clone = script.Parent:WaitForChild("LightPart")
	TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, pos)}):Play()
	print(pos)
end

RS.Light.OnServerEvent:Connect(function(player, bool)
	local Clone = script.Parent:WaitForChild("LightPart")
	if bool then
		TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
	else
		TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
	end
end)

This should allow all players to see the light moving as the player moves their mouse.

I hope this helps! Let me know if you have any questions.

Alright, let me test this out, thanks.

Could you send the layout? I think you used Remote Functions.

Edit: Output prints mouse position, but the light doesn’t show.

ill try make it just wait amoment

The output prints mouse position, however, the light doesn’t move.

how i upload the flash light here

You don’t need to upload it. Could you, however, help me fix this:

I tried replacing the tween with Clone.CFrame = CFrame.lookAt(Clone.Position, pos), but the player glitches out.

remove the weld and try agine 30 ch

I removed the weld, what do I do?

image

try make the part go to the mouse hit position and use magntude to make max travel distance

Thanks, do you know how I can make the light part attach to the handle without welds?

lol you get it.

I may have a theory but I’m not sure

If you figure something out, please let me know! This is pretty important.