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
script.Sound:Play()
else
plrChar.MinerHat.Light.SpotLight.Enabled = false
plrChar.MinerHat.Light.Material = "Plastic"
toggle = false
end
end
Button.MouseButton1Click:Connect(OpenAndClose)
Use a remote event to fire the server with the desired state:
local remote = path.to.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
remote:FireServer(state)
end
button.MouseButton1Click:Connect(toggle)
Now, on the server,
local remote = path.to.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"
end)
If you’re unfamiliar with remotes, you should read this as an introduction to remote events.