Solution
You can use a RemoteEvent
. For this one the RemoteEvent
should be in ReplicatedStorage
and named PoliceLights. Create a normal script in ServerScriptService
as well.
What is a RemoteEvent?
If you don't know what RemoteEvent
s are, they are used to send data Client-Server and vice versa. The Server is the game itself and the Client is the player themself. Normal Scripts run in the Server while LocalScript
s and Normal Scripts that their RunContext
is set to Client run in the Client. If something is changed in the Server, it will also show up in all Clients, while if something is changed in one Client, it will only show in that Client. RemoteEvent
s crosses that boundary and makes it that if you want to change something in the Server in a Client script and vice versa, you can actually achieve it.
Tips & Extra Solutions
Tip: You don't need to connect UserInputService.InputBegan
twice to make two parts change at the same time, you can use task.spawn()
to make them run at the same time.
Tip 2: If you press E multiple times, it might glitch the two parts. To fix that, we will use a debounce in which we will wait for the two parts to finish glowing before pressing E again.
Tip 3: If you type E in the chat, it will activate the two parts, we will use the second parameter of UserInputService.InputBegan
's callback function gameProcessedEvent which is a boolean (true/false) that tells if it was processed by the game (e.g., typing E in the chat or in a TextBox
), and check if it's true, and if it is, we will immediately stop the callback function using return
.
Tip 4: Use task.wait()
instead of wait()
.
Final Script
Normal Script in ServerScriptService
:
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local RemoteEvent = ReplicatedStorage.PoliceLights |
04 | local goodpart 1 = workspace.transporterT 4. Body.biglightbar.unionright |
05 | local goodpart 2 = workspace.transporterT 4. Body.leftlightbar.main |
08 | RemoteEvent.OnServerEvent:Connect( function () |
09 | if pressedE = = false then |
13 | goodpart 1. BrickColor = BrickColor.new( "Medium blue" ) |
14 | goodpart 1. Material = Enum.Material.SmoothPlastic |
16 | goodpart 1. Material = Enum.Material.Neon |
18 | goodpart 1. Material = Enum.Material.SmoothPlastic |
20 | goodpart 1. Material = Enum.Material.Neon |
22 | goodpart 1. Material = Enum.Material.SmoothPlastic |
24 | goodpart 1. Material = Enum.Material.Neon |
26 | goodpart 1. Material = Enum.Material.SmoothPlastic |
29 | goodpart 2. BrickColor = BrickColor.new( "Medium blue" ) |
30 | goodpart 2. Material = Enum.Material.Neon |
32 | goodpart 2. Transparency = 0 |
34 | goodpart 2. Transparency = 1 |
36 | goodpart 2. Transparency = 0 |
38 | goodpart 2. Transparency = 1 |
40 | goodpart 2. Transparency = 0 |
42 | goodpart 2. Transparency = 1 |
44 | goodpart 2. Transparency = 0 |
LocalScript:
01 | local key = game:GetService( "UserInputService" ) |
03 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 | local RemoteEvent = ReplicatedStorage:WaitForChild( "PoliceLights" ) |
06 | key.InputBegan:Connect( function (input, gameProcessedEvent) |
07 | if gameProcessedEvent = = true then return end |
09 | if input.KeyCode = = Enum.KeyCode.E then |
10 | RemoteEvent:FireServer() |