So, I’ve been wanting to make a button where when you press it, it changes the material of a part but only the client can see changes and not the server. But I tried making it by using remote events and a server script but it didn’t work. Any help?
Well you need to use a remote event, I am guessing you did It incorrectly since you say it didn’t work.
Also could you show us your code that didn’t work.
You don’t need to utilise remote events for this, since everything you mentioned is clientsided.
--Local script
local part = game.Workspace.Part -- Reference to the part
local button = script.Parent --Local script inside a button
button.Activated:Connect(function()
part.Material = Enum.Material.Grass -- Change this to whatever you need
end)
Okay.
Here’s the code for the ServerScript:
game.StarterGui.DisableGui.Disable.MouseButton1Click:Connect(function()
game.ReplicatedStorage.DisableTextures:FireClient()
end)
Here’s the code for the button:
local grass = game.Workspace.Grass
game.ReplicatedStorage.DisableTextures.OnClientEvent:Connect(function()`
grass.Material = Enum.Material.SmoothPlastic
end)
You are using the remote event the wrong way, you are doing server to client instead of client to server.
You shouldn’t check if a gui button is clicked on the server. Instead do something like this:
-- Local script inside of screen gui
script.Parent.TextButton.MouseButton1Click:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireServer()
end)
--Script inside of serverscriptservice
local grass = game.Workspace.Grass
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
grass.Material = Enum.Material.SmoothPlastic
end
The title says you want the change to happen on the client, but you say it happens on the client but not on the server.
Do you want the changes on the client or server?
I want to have changes to the client.
Then you would simply do:
local grass = game.Workspace.Grass
script.Parent.TextButton.MouseButton1Click:Connect(function()
grass.Material = Enum.Material.SmoothPlastic
end)
If it’s ONLY the client, you don’t use remote events. Just change that part’s material in the local script.
Oh. I used remote events cause I thought that was the only way to do it for changing the environment.
What do you mean by changing the environment?
Do you mean workspace?
You should know that remove events ALLOWS a client and a server to talk each other. Only use this if you want the part to be the same as your screen and everyone else’s.
Uh, yes. I mean by that. Sorry if that confused you.
If you want it to be toggable then you can you this code:
local grass = game.Workspace.Grass
local texture = true
local debounce = false
script.Parent.TextButton.MouseButton1Click:Connect(function()
if not debounce then
return
end
debounce = true
wait(0.5)
if texture == true then
grass.Material = Enum.Material.SmoothPlastic
else
grass.Material = Enum.Material.Grass
end
texture = not texture
end)
Or you could use some boolean logic instead of an if statement
grass.Material = texture and Enum.Material.Grass or Enum.Material.SmoothPlastic
Edit: Debounce added, thanks @ItzMeZeus_IGotHacked
Don’t forget to add a debounce! It prevents it spamming the material to change. ![]()
You don’t really need a debounce for toggling in my opinion
Thanks everyone for helping me solve my problem and teaching me a bit about client events.
You do need it, try making a button which prints Hi in your studio without the debounce. It should print like 12 times with just a click.
Remember to mark the post which you think had the solution as the solution so others know that the problem is solved.
Why would it print 12 times?
https://gyazo.com/25667802d725bb7123cec5825c4122cb