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 1 year ago
Edited 1 year 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:

local goodpart = game.Workspace.transporterT4.Body.biglightbar.unionright
local key = game:GetService("UserInputService")

key.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        goodpart.BrickColor = BrickColor.new("Medium blue")
        goodpart.Material = Enum.Material.SmoothPlastic
        wait(0.15)
        goodpart.Material = Enum.Material.Neon
        wait(0.15)
        goodpart.Material = Enum.Material.SmoothPlastic
        wait(0.15)
        goodpart.Material = Enum.Material.Neon
        wait(0.15)
        goodpart.Material = Enum.Material.SmoothPlastic
        wait(0.15)
        goodpart.Material = Enum.Material.Neon
        wait(0.15)
        goodpart.Material = Enum.Material.SmoothPlastic
    end
end)

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

local goodpart = game.Workspace.transporterT4.Body.leftlightbar.main
local key = game:GetService("UserInputService")

key.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        goodpart.BrickColor = BrickColor.new("Medium blue")
        goodpart.Material = Enum.Material.Neon
        wait(0.15)
        goodpart.Transparency = 0
        wait(0.15)
        goodpart.Transparency = 1
        wait(0.15)
        goodpart.Transparency = 0
        wait(0.15)
        goodpart.Transparency = 1
        wait(0.15)
        goodpart.Transparency = 0
        wait(0.15)
        goodpart.Transparency = 1
        wait(0.15)
        goodpart.Transparency = 0
    end
end)

1 answer

Log in to vote
1
Answered by 1 year 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:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.PoliceLights

local goodpart1 = workspace.transporterT4.Body.biglightbar.unionright
local goodpart2 = workspace.transporterT4.Body.leftlightbar.main

local pressedE = false -- this will be our debounce
RemoteEvent.OnServerEvent:Connect(function() -- this callback function will run if RemoteEvent:FireServer() in the Client successfully reached the Server
    if pressedE == false then -- if E hasn't been pressed yet
        pressedE = true -- it will tell that E has been pressed

        task.spawn(function()
            goodpart1.BrickColor = BrickColor.new("Medium blue")
            goodpart1.Material = Enum.Material.SmoothPlastic
            task.wait(0.15)
            goodpart1.Material = Enum.Material.Neon
            task.wait(0.15)
            goodpart1.Material = Enum.Material.SmoothPlastic
            task.wait(0.15)
            goodpart1.Material = Enum.Material.Neon
            task.wait(0.15)
            goodpart1.Material = Enum.Material.SmoothPlastic
            task.wait(0.15)
            goodpart1.Material = Enum.Material.Neon
            task.wait(0.15)
            goodpart1.Material = Enum.Material.SmoothPlastic
        end)

        goodpart2.BrickColor = BrickColor.new("Medium blue")
        goodpart2.Material = Enum.Material.Neon
        task.wait(0.15)
        goodpart2.Transparency = 0
        task.wait(0.15)
        goodpart2.Transparency = 1
        task.wait(0.15)
        goodpart2.Transparency = 0
        task.wait(0.15)
        goodpart2.Transparency = 1
        task.wait(0.15)
        goodpart2.Transparency = 0
        task.wait(0.15)
        goodpart2.Transparency = 1
        task.wait(0.15)
        goodpart2.Transparency = 0

        pressedE = false -- it will tell that E hasn't been pressed yet
    end
end)

LocalScript:

local key = game:GetService("UserInputService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("PoliceLights")

key.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent == true then return end

    if input.KeyCode == Enum.KeyCode.E then
        RemoteEvent:FireServer()
    end
end)
0
yeah like he said HeroFigo 81 — 1y
Ad

Answer this question