Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

luau How do i make it so it show up to other players?

Asked by 2 years ago
Edited 2 years ago

Hi, first of all i am very sorry if this question is dumb or smth but i'm very new to coding in roblox.

So i made this script (and i made 4 of these for every light) so when i press E on the keyboard the police lights on this car flash for few seconds and then stop but when i wanted to show it to my friends i saw that when i clicked the ubtton it showed it flashing only to me so it just shows up only to the player that clicks the key and i dont know how to make it so it will show to every player.

I think the issue is because this script is in a LocalScript not normal Script but when i paste it in to a normal script it doesnt work pelase help.

Also the scripts are located in StartedGui

script for the two big light in the big lightbar:

01local goodpart = game.Workspace.transporterT4.Body.biglightbar.unionright
02local key = game:GetService("UserInputService")
03 
04key.InputBegan:Connect(function(input)
05    if input.KeyCode == Enum.KeyCode.E then
06        goodpart.BrickColor = BrickColor.new("Medium blue")
07        goodpart.Material = Enum.Material.SmoothPlastic
08        wait(0.15)
09        goodpart.Material = Enum.Material.Neon
10        wait(0.15)
11        goodpart.Material = Enum.Material.SmoothPlastic
12        wait(0.15)
13        goodpart.Material = Enum.Material.Neon
14        wait(0.15)
15        goodpart.Material = Enum.Material.SmoothPlastic
View all 21 lines...

script for the 2 small lightbars on the back of the car:

01local goodpart = game.Workspace.transporterT4.Body.leftlightbar.main
02local key = game:GetService("UserInputService")
03 
04key.InputBegan:Connect(function(input)
05    if input.KeyCode == Enum.KeyCode.E then
06        goodpart.BrickColor = BrickColor.new("Medium blue")
07        goodpart.Material = Enum.Material.Neon
08        wait(0.15)
09        goodpart.Transparency = 0
10        wait(0.15)
11        goodpart.Transparency = 1
12        wait(0.15)
13        goodpart.Transparency = 0
14        wait(0.15)
15        goodpart.Transparency = 1
View all 23 lines...

1 answer

Log in to vote
1
Answered by 2 years ago

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 RemoteEvents 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 LocalScripts 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. RemoteEvents 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:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local RemoteEvent = ReplicatedStorage.PoliceLights
03 
04local goodpart1 = workspace.transporterT4.Body.biglightbar.unionright
05local goodpart2 = workspace.transporterT4.Body.leftlightbar.main
06 
07local pressedE = false -- this will be our debounce
08RemoteEvent.OnServerEvent:Connect(function() -- this callback function will run if RemoteEvent:FireServer() in the Client successfully reached the Server
09    if pressedE == false then -- if E hasn't been pressed yet
10        pressedE = true -- it will tell that E has been pressed
11 
12        task.spawn(function()
13            goodpart1.BrickColor = BrickColor.new("Medium blue")
14            goodpart1.Material = Enum.Material.SmoothPlastic
15            task.wait(0.15)
View all 48 lines...

LocalScript:

01local key = game:GetService("UserInputService")
02 
03local ReplicatedStorage = game:GetService("ReplicatedStorage")
04local RemoteEvent = ReplicatedStorage:WaitForChild("PoliceLights")
05 
06key.InputBegan:Connect(function(input, gameProcessedEvent)
07    if gameProcessedEvent == true then return end
08 
09    if input.KeyCode == Enum.KeyCode.E then
10        RemoteEvent:FireServer()
11    end
12end)
0
yeah like he said HeroFigo 81 — 2y
Ad

Answer this question