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

How to use control server objects with localscripts?

Asked by 4 years ago

So I am working on a game that uses localscripts to detect the player pressing a button while on a seat and it works perfectly for the client. The way it works is when you sit on the seat and press l, the lights for the car turn on/off. The issue is that localscripts only control things for the local client. So no other players can see the lights when they are on. How can I fix this? I have heard things about "triggers" and I have no idea what they are or how to use them. Heres my code, How can I adapt it to make the server change the parts? This script is located in StarterGUI and is disabled until the player sits on the correct seat. It is disabled again when the player gets up.

local player = game.Players.LocalPlayer
local userInput = game:GetService("UserInputService")
local speed = 25
local toggle = 0
FunctionParts = workspace.PickUpTruck.Vehicle.FunctionParts

userInput.InputBegan:Connect(function(key) -- Toggle lights on/off
    if key.KeyCode == Enum.KeyCode.L then
        if toggle == 0 then
            toggle = 1
            FunctionParts.Light1.Material = "Neon"
            FunctionParts.Light1.SpotLight.Enabled = true
            FunctionParts.Light2.Material = "Neon"
            FunctionParts.Light2.SpotLight.Enabled = true
            FunctionParts.Light3.Material = "Neon"
            FunctionParts.Light3.SpotLight.Enabled = true
            FunctionParts.Light4.Material = "Neon"
            FunctionParts.Light4.SpotLight.Enabled = true
        elseif toggle == 1 then
            toggle = 0
            FunctionParts.Light1.Material = "Plastic"
            FunctionParts.Light1.SpotLight.Enabled = false
            FunctionParts.Light2.Material = "Plastic"
            FunctionParts.Light2.SpotLight.Enabled = false
            FunctionParts.Light3.Material = "Plastic"
            FunctionParts.Light3.SpotLight.Enabled = false
            FunctionParts.Light4.Material = "Plastic"
            FunctionParts.Light4.SpotLight.Enabled = false
        end

    end
end)

Thank you to anyone who can help or tried to help.

1
You can use Remote Event for that, look it up on Youtube Nguyenlegiahung 1091 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You could have a remote event. Go in your explorer find ReplicatedStorage and add a RemoteFunction. Then make a new script inside ServerScriptService:

local replicatedStorage = game:GetService("ReplicatedStorage")
FunctionParts = workspace.PickUpTruck.Vehicle.FunctionParts
replicatedStorage.RemoteFunction.OnServerInvoke = function(player, toggle)
    if toggle == 0 then
        toggle= 1
        FunctionParts.Light1.Material = "Neon"
                FunctionParts.Light1.SpotLight.Enabled = true
            FunctionParts.Light2.Material = "Neon"
            FunctionParts.Light2.SpotLight.Enabled = true
            FunctionParts.Light3.Material = "Neon"
            FunctionParts.Light3.SpotLight.Enabled = true
            FunctionParts.Light4.Material = "Neon"
            FunctionParts.Light4.SpotLight.Enabled = true
       elseif toggle == 1 then
                toggle= 0
                FunctionParts.Light1.Material = "Plastic"
                FunctionParts.Light1.SpotLight.Enabled = false
                FunctionParts.Light2.Material = "Plastic"
                FunctionParts.Light2.SpotLight.Enabled = false
                FunctionParts.Light3.Material = "Plastic"
                FunctionParts.Light3.SpotLight.Enabled = false
                FunctionParts.Light4.Material = "Plastic"
        FunctionParts.Light4.SpotLight.Enabled = false
    end
    return toggle
end

And then in your current script you should have :

local replicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local userInput = game:GetService("UserInputService")
local speed = 25
local toggle = 0
FunctionParts = workspace.PickUpTruck.Vehicle.FunctionParts
userInput.InputBegan:Connect(function(key) -- Toggle lights on/off
        if key.KeyCode == Enum.KeyCode.L then
        toggle=replicatedStorage.RemoteFunction:InvokeServer(toggle)
        print(toggle)
        end

end)

Im not an expert.. I hope you can understand it :) heres a guide on remote functions guide hope this helps!

1
Thanks for your help, worked perfectly :) BlockBob12 3 — 4y
0
Awesome, no worries ! Mast3rStRix 43 — 4y
Ad

Answer this question