How to make this server-sided?

i found this script in the toolbox and it works as intended, but i was wondering how to make it server-sided? i know how to use both remote events and functions but i completely clueless how to approach this lol

pls help :sob:

wait(1)

local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Character = Player.Character
local FlashlightOn = false



local FlashlightCamera = Instance.new("Part")
FlashlightCamera.Anchored = true
FlashlightCamera.Parent = Camera
FlashlightCamera.Transparency = 1
FlashlightCamera.Name = "FlashlightCamera"
FlashlightCamera.CanCollide = false



local FlashLIGHT = Instance.new("SpotLight")
FlashLIGHT.Parent = FlashlightCamera
FlashLIGHT.Enabled = false
FlashLIGHT.Range = 60
FlashLIGHT.Brightness = 2


local function ToggleFlashlight(input, prosecced)
	if not FlashlightOn and not prosecced and input.KeyCode == Enum.KeyCode.F then
		script.Sound:Play()
		FlashLIGHT.Enabled = true
		FlashlightOn = true
	elseif FlashlightOn and not prosecced and input.KeyCode == Enum.KeyCode.F then
		script.Sound:Play()
		FlashLIGHT.Enabled = false
		FlashlightOn = false
	end
end

UserInputService.InputBegan:Connect(ToggleFlashlight)

Character:WaitForChild("Humanoid").Died:Connect(function()
	FlashlightCamera:Destroy()
	FlashLIGHT:Destroy()
end)

RunService.RenderStepped:Connect(function()
	TweenService:Create(FlashlightCamera, TweenInfo.new(0.2), { CFrame = Camera.CFrame }):Play()
end)
2 Likes

you’d need to use RemoteEvents to make the flashlight server-sided; on the codes above:

  1. you first need to instantiate the required camera part and spotlight
  2. fire the remote with an argument that determines whether or not the flashlight is enabled (true/false)
  3. in the server; listen for flashlight request and turn on/off the player’s flashlight
  4. The .Died connection seems useless so ignore that.
1 Like

in order for the flashlight to work overall it uses this line of code

ts:Create(flashlight, TweenInfo.new(0.2), {CFrame = cam.CFrame}):Play()

but if i were to fire a remoteEvent to the server, the server cannot access the player’s camera, let alone it’s CFrame

If you wish the light to move server-side, I would highly recommend using networkownership. Your other option would be to fire a remote every renderstepped, which i don’t consider the most practical.

I did write a post about networkownershop a few years ago, but it is still relevant today: Network ownership for beginners - How to handle physics on the client

i’ve heard of network ownership but never implemented it before. from your post it states - you can only set unanchored part to have network ownership to the client, but I’m afraid my flashlight part can’t as it keeps phasing through the ground :,)

correct me if i’m wrong, but this was my attempt:

SERVER

function remoteFunctions.InsertLight.OnServerInvoke(plr)
	local flashlight = replicated.Misc.Flashlight:Clone()
	flashlight.Parent = plr.Character
    flashlight:SetNetworkOwner(plr)
end

CLIENT

remoteFunctions.InsertLight:InvokeServer()
local flashlight = char:WaitForChild("Flashlight")
flashlight.Parent = cam

Yes, the implementation is correct, but the part phasing through the ground is an issue.

A part, that has it’s network owner as a player, can not be anchored. To overcome the issue of the part just falling through the ground, I would use bodymovers. For your usecase, AlignPosition and AlignOrientation might both be beneficial.

sorry :,) i’m not experienced enough to understand how bodymover works let alone implementing them. perhaps, you could show me some snippet? if not then it’s okay :slight_smile:

It’s quite complex, but it’s worth learning it – I use them in almost all the games I make. Best you can do is either search about it or ask in another topic about them.