Help with local scripts, remote events and server scripts

Hey, WiseMatheu here!

I have a script which turns lights on and off but it only does it locally (for the player that presses the TextButton. I am wondering how I can use remote events and server scripts to make the change for the whole server and not just the local player.

Thanks for your time,
WiseMatheu :slight_smile:

Here’s an example script:

LocalScript: (assume this is inside a GuiButton)

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Button = script.Parent

--//Functions
Button.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer() --//Fires the event
end)

ServerScript:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Light = workspace.Part.PointLight --//Reference your light

--//Functions
RemoteEvent.OnServerEvent:Connect(function(player) --//The player argument is passed in automatically

    --//Makes the lights brightness to 100
	Light.Brightness = 100 
    
    --//OR you can enable the light
    Light.Enabled = true
end)
2 Likes

So if you want to change something for the whole server then you need to put remote event to ReplicatedStorage.

After this put a local script for example to StarterGui:

local change = "change for whole server"
wait()
game.ReplicatedStorage.RemoteEvent:FireServer(change)

Now you have done first step, you gave info to your remove event. Now remote event needs to collect it correctly, so put a script (not local script) for example to StarterGui.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(change)
   print(change)
end)

That’s an example of how you can use it.

Thanks for both of your solutions, I have marked @Katrist’s as the solution as it was posted first but thanks @FrozenTheCreator as well for your reply.

Have a nice day,
WiseMatheu :slightly_smiling_face:

1 Like

image

How would I get the exhibitRoom variable across to the server script?

if room == "Exhibit" then
    local exhibitRoom = script.Parent.Parent.ExhibitRoom.Text
    exhibitEvent:FireServer(exhibitRoom)
end
game.ReplicatedStorage:FindFirstChild("").OnServerEvent:Connect(function(exhibitRoom)
    -- Do whatever with the exhibitRoom variable! It will have sent the text to the server.
end)

So I put the exhibitRoom in a set of brackets in the local script and in a set of brackets in the server script?

If that’s the name of the RemoteEvent inside replicated storage, yes.

Alright thank you. One more thing then I think I’m done, how would you carry across 2 variables? Would you separate them by a comma?

Yep! This text will be blurred

Yea, just like this:

Localscript:

if room == "Exhibit" then
    local exhibitRoom = script.Parent.Parent.ExhibitRoom.Text
    exhibitEvent:FireServer(exhibitRoom, "extraInfo")
end

ServerScript:

game.ReplicatedStorage.exhibitEvent.OnServerEvent:Connect(function(player, text, info) 
    print(text .. info)
end)

Also for the serverscript, the player variable is automatically passed through so your going to have to reference it first.

So I’d have to do player, and then anything else I want it to bring over?

Yes.

30lett

Yay it works! Thanks everyone who helped me with my issue.