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)
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)
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)
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.
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 ¯\__(ツ)__/¯
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