Help with server-side flashlight

Hi, i’m trying to make a server side flashlight but can’t seem to figure it out, i haven’t worked with remote events that much before so it all seems a little complicated to me.

I made a flashlight but it’s only client-side.

The code:

local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")

local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)

local cam = workspace.CurrentCamera

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local mouse = plr:GetMouse()

local tool = script.Parent

local batteryLevel = char:WaitForChild("BatteryLevel")
local bool = tool:WaitForChild("On")

local flashlight = RS.flashlight
local Clone = flashlight:Clone()

local Brightness = 5

Clone.Parent = char

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 bool.Value == true then
			TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
		elseif bool.Value == false then
			TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
		elseif batteryLevel.Value <= 0 then
			TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
		end

	end
end)

1 Like

Try copy the script and paste it into a server script and for player just write game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
(if its directly inside the flashlight)

doesn’t work, the output prints out “Attempt to index nil with “Hit”” line 29

Or make a server script in ServerScriptService and put a RemoteEvent inside the ReplicatedStorage Then copy the whole RenderSteped part paste it into the server script.
Server script:

game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(player, tool, cam)

local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")

local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local batteryLevel = char:WaitForChild("BatteryLevel")
local bool = tool:WaitForChild("On")

local flashlight = tool
local Clone = flashlight:Clone()

local Brightness = 5

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 bool.Value == true then
			TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
		elseif bool.Value == false then
			TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
		elseif batteryLevel.Value <= 0 then
			TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
		end

	end
end)
end)

LocalScript(inside the tool):

local tool = script.Parent
local cam = workspace.CurrentCamera

tool.Equipped:Connect(function()
    game:GetService("ReplicatedStorage"):WaitForChild("RmoteEvent"):FireServer(tool, cam)
end)

Why do you want to make this server-side? Visual effects should always be client-sided, so that:

  1. The server has less things to worry about and therefore reduces network usage and increases performance
  2. The effects are smooth and dependent on the player’s own device instead of a potentially bad and laggy internet connection

Because it’s a horror game, if your flashlight runs out of battery then you have to rely on your friend’s flashlight

Ok, but why does it need to be server-side? You can have the client disable the flashlight when it reaches 0 charge, and have the client know what charge the flashlight has through the server changing an attribute.

Is this an attempt to combat exploits?

Yeah, so the player’s can’t get unlimited battery for example. i shouldn’t trust the client

I figured it out, it was really simple.
I had a local script always firing a remote event sending the camera’s position and mouse’s position and applied it with a server script

Exploiters can still do that by creating their own light objects. You aren’t losing anything by making the flashlight be more responsive by handling it on the client ¯\__(ツ)__/¯

I can just detect if a new light object is added and remove it

So it won’t lag on the client side i made a flashlight part for the client and one for the server, so the client part will only show to the client and the server part will only show to the server

Exploiters can always bypass client checks. They could also completely disable the lighting system, making everything look fully-lit:

(look closely, on Kreekcraft’s perspective, the door is completely black, but on the exploiters perspective, it’s lit up and perfectly visible)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.