How to make this script Server and Client Side

How do I make this script Client and Server sided?
and how am I going to write it?

local player = game.Players.LocalPlayer
local plrChar = player.Character

local button = script.Parent

local toggle = false

function OpenAndClose()
	if not toggle then
		plrChar.MinerHat.Light.SpotLight.Enabled = true
		plrChar.MinerHat.Light.Material = "Neon"
		toggle = true
		plrChar.MinerHat.Light.SpotLight.Enabled = false
		plrChar.MinerHat.Light.Material = "Plastic"
		toggle = false


This is a localScript in StarterGui

Use a remote event to fire the server with the desired state:

local remote = -- probably in replicated storage

local state = false -- it's off
local function toggle()
    state = not state -- basically a shorthand version of doing if state == true then state = false else state = true
    -- technically you don't need these next two lines but if you want to give the illusion of instantaneously turning the lamp off/on then you can do it:
    plrChar.MinerHat.Light.SpotLight.Enabled = state
	plrChar.MinerHat.Light.Material = state and "Neon" or "Plastic" -- if state then neon else plastic

Now, on the server,

local remote =
remote.OnServerEvent:Connect(function(player, state)
    local character = player.Character
    plrChar.MinerHat.Light.SpotLight.Enabled = state
	plrChar.MinerHat.Light.Material = state and "Neon" or "Plastic"

If you’re unfamiliar with remotes, you should read this as an introduction to remote events.

1 Like

Ok, I tried it but it didn’t work. After some double-checking, It worked perfectly

Thank you for helping!

1 Like