Move local script to the server side

I made a script for the flashlight on the client side but I’m having trouble moving it to the server. Can anyone help?

I wanted to make a server-side script to make the light visible to other players but my skills are a bit poor.

I tried to look at similar topics in the dev forum and found answers, but my attempts to transfer the script to the server side resulted in the script not working or doing something strange.

Here’s the code

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

function Light()
	local player = game.Players.LocalPlayer
	local playerChar = player.Character
	local playerLight = playerChar.HumanoidRootPart:FindFirstChild("Light")
	local playerSound = playerChar.HumanoidRootPart:FindFirstChild("Sound")
	if playerLight then
		playerSound:Destroy()
		playerLight:Destroy()
		local play = Instance.new("Sound",playerChar:FindFirstChild("HumanoidRootPart"))
		play.SoundId = "http://www.roblox.com/asset/?id=6020984334"
		play:Play()
	else
		light = Instance.new("SpotLight",playerChar:FindFirstChild("HumanoidRootPart"))
		light.Name = "Light"
		light.Range = 30
		light.Brightness = 1
		light.Shadows = true
		local play = Instance.new("Sound",playerChar:FindFirstChild("HumanoidRootPart"))
		play.SoundId = "http://www.roblox.com/asset/?id=6020984334"
		play:Play()

	end
end

mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "f" then
		Light()
	end
end)

In short, it adds SpotLight to the player’s body when the “F” key is pressed and also removes it when the “F” key is pressed again. It uses sound, but for now my goal is to translate the script itself and not the sound to the server side.

You can use a RemoteEvent to transfer creating the light on the server. In your Light function, fire your RemoteEvent, and on the server you basically just have to copy and paste the code for handling the light from your local script.

remote.OnServerEvent:Connect(function(player)
   local playerChar = player.Character
   local playerLight = playerChar.HumanoidRootPart:FindFirstChild("Light")
   if playerLight then
		playerLight:Destroy()
	else
		local light = Instance.new("SpotLight",playerChar.HumanoidRootPart)
		light.Name = "Light"
		light.Range = 30
		light.Brightness = 1
		light.Shadows = true
	end
   
end);

Mhm. It’s working properly on the server side now. Thank you

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