Hello, as the title says, how I can do that? I would like to use it for code test and special items for other users.
Have you tried doing anything yet?
To begin with, you should ask yourself a question: how to interact with the entire server from a local script.
If you want the colour to change only on the client, then all you need is to connect to the .Activated
event of the button:
local button = script.Parent -- Button location
local colouredPart = workspace.Part -- Part to change colour
button.Activated:Connect(function() -- Detect when the button is clicked
colouredPart.Color = Color3.new(1, 0, 0) -- Change the colour to red
end)
If you want to change the colour on the server, so all clients see the change then you can fire a RemoteEvent
when the button is clicked that the server connects to:
local button = script.Parent -- Button location
local clickEvent = button.ClientClicked -- Event to communicate with server
button.Activated:Connect(function() -- Detect when the button is clicked
clickEvent:FireServer() -- Fire the event to tell the server
end)
and then change the colour on the server by connecting to the .OnServerEvent
signal:
local clickEvent = script.Parent.ClientClicked -- Event to communicate with server
local colouredPart = workspace.Part -- Part to change colour
clickEvent.OnServerEvent:Connect(function() -- Listen for the RemoteEvent
colouredPart.Color = Color3.new(1, 0, 0) -- Change the colour to red
end)
If the client-server model/replication is new to you, I would recommend reading the following articles:
First of all, you must create a parameter such as RemoteEvent in ReplicatedStorage and name it whatever you like (in my case it is called ChangeColorEvent), in the Workspace Part itself, respectively.
In the button you have to create a local script and write the following:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeColorEvent = ReplicatedStorage.ChangeColorEvent
-- When player pressed the button:
script.Parent.MouseButton1Click:Connect(function()
ChangeColorEvent:FireServer()
end)
Then we create a script in ServerScriptService and write the following:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeColorEvent = ReplicatedStorage.ChangeColorEvent
-- Our Part
local Part = workspace.Part
-- Getting event from local script
ChangeColorEvent.OnServerEvent:Connect(function()
Part.BrickColor = BrickColor.random() -- Or any colors
end)
In this case, the entire server will see the Part color change.
Put this code in a script. Put that script inside a gui button for it to work.
script.Parent.MouseButton1Click:connect(function() --Click detector
local part = game.Workspace.Part --The part you want to change
local color = BrickColor.new(1, 1, 1)
part.BrickColor = color
end)
It’s working only in a local client.