How do i make the lights toggleable and serversided?

the code is here

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteevent = ReplicatedStorage:WaitForChild("CarControl")
local UserInputService = game:GetService("UserInputService")
local lightsOn = false
local inCar = false
local car 

humanoid.Seated:Connect(function(seated, seat)
    if seat and seat.Parent.Parent.Name == "Car" then
        inCar = true
    else
        inCar = false
    end
	car = seat.Parent.Parent
end)
UserInputService.InputBegan:Connect(function(input, processed)
    if input.KeyCode.Name == "L" and not processed and inCar then
	local Headlights = {car.Headlights.Headlight1.SurfaceLight, car.Headlights.Headlight2.SurfaceLight, car.Headlights.Headlight3.SurfaceLight, car.Headlights.Headlight4.SurfaceLight}
		lightsOn = not lightsOn
		for i, v in pairs(Headlights) do
  			v.Enabled = true
		end
        print("big chungus lover toggled their lights") 
    end
end)

You will need use remote events and remote functions to communicate with the server.
Here is the article to remote functions/events if you are not familiar on them.

Since you don’t require return data from the server you should use a remote event to tell a server script to turn on the car lights after you have done that locally.

2 Likes

Ok but i’d need to locate the lights which i cannot do anymore or hoa am i going to locate the current cars lights,and how am i going to tell it to turn the lighrs on, like i cant just write the direction and lights on can i

I assume the lights themselves aren’t local in your case so you can just pass the lights trough the event as arguments. For example: remoteEvent:FireClient(Headlights, lightsOn).
In the server script you would then turn the lights on or off using these arguments.

1 Like

don tyou mean “FireServer”? since its client to server

Yes FireServer, I made a mistake there.

1 Like

so how am i going to locate the car the local player is sitting on in the server script (there will be multiple cars called “Car” and they are spawned using a button

Don’t you already know which lights you need to address when you turn on the lights locally?
Pass these lights to the server script via the remote event argument.

1 Like

so the server script will know what im talkinga bout when reusing the same set variables?

If you pass them through the event, then yes.

1 Like

Source:

When you want to turn the lights on/off you can use a remote event.
As you want it to change on the server you would want to do: Event:FireServer(Variables)

With the variables I would add in some kind of password to prevent people abusing your lights. But when you pass on the variables it will have the information the server requires.

So on the server you can do:
Event.OnServerEvent:Connect(function(Variables)

end)

Inside the Server Event I would check for the password, and then if its saying “Hallway Lights On” then you could do the hallway lights.(You can play with this a bit to get some more control over the lights).

When passsing on variables through remote events I recommend putting them into an array as it makes life a bit easier. When you work with them a bit more you will understand why. For Example:

— Client
local Data = {Password,Player,Action}
Event:FireServer(Data)

— Server
Event.OnServerEvent:Connect(function(Data)
if Data[1] == “Password” then
– And so on
end
end)

So you can then fire the server with Data as your only variable and reduce errors and make it easier and shorter to script and work with. (I have some Data collectors that have tons of variables, so if i was to put each one in, there would be major issues and a load of wasted time).

I already got it figured out with the serversided, how do i make it toggle OFF every 2nd time when L is pressed,
(2,4,6,8…)
And toggle on every 2nd time (1,3,5,7,9…)

so you would need to have some sort of way to get when the key is pressed, so using Mouse.KeyDown:Connect(function(Key)

end)

Will allow you to see which key is pressed. So if the lights are currently on you can do it a easy way and just see if Part.Light.Enabled == true. If it is true that means your light is on. so when you press it this time it should turn off the light

so if light.Enabled == false when you press L it will turn on

if Light.Enabled == true then when you press L it will turn off

1 Like

great explanation to make me understand things!

1 Like