Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a brick appear or disappear if you press a key?

I'm pretty terrible at scripting I've been trying to make a part appear if you press the key r on your keyboard.

Player = game.Players.LocalPlayer

gui = script.Parent

Mouse = Player:GetMouse()

Box = script.brick

Open = false

  

Mouse.KeyDown:connect(function(key)

if (key == "r") then

if (Open == false) then

Box.Transparency = 0

Open = true

elseif (Open == true) then

Box.Transparency = 1

Open = false

end

end

end)

1 comment

0

I got an idea. Put the brick in replicated storage instead of the script.Then say game:GetService("ReplicatedStorage"):FindFirstChild("Brick"):Clone() And then do Brick.Parent = game.Workspace. And what is the problem here, like what exactly? What does the output say?

2 answers

Hi Emorch, For this, you need to put the brick in workspace somewhere and make a local script on the client-side. Client-side meaning somewhere in StarterGui, StarterPack, or StarterPlayer. Then, you just need to locate the part in the server and just change it using UserInputService and a boolean value to change the two states of a function. Just a heads up that this will not work with FilteringEnabled, to make it work with FilteringEnabled you need to just use some RemoteEvents or RemoteFunctions to activate a script that makes the part transparent/opaque. So, the whole thing would end up looking like this:

local part = workspace:WaitForChild("Brick") -- The part in workspace.
local uis = game:GetService("UserInputService") -- The UserInputService service, this will be used to get the keyboard interaction. Better to use UserInputService rather than KeyDown, KeyDown is deprecated.
local active = false; -- Means that the function is not active. Used as a switch to turn between the two states.

uis.InputBegan:Connect(function(obj, gp) -- Anonymous function that is being binded to the event. Obj is the Input Object and gp is Game Processed which is true if the user pressed the key while typing.
    if (obj.KeyCode == Enum.KeyCode.R) and (not gp) then -- Checks if the key pressed is R and if it was pressed while it was meant to be pressed, meaning that it wasn't pressed while typing in chat or something.
	    if not active then -- Checks if its inactive, if the function is inactive then the part becomes invisible since the function is about to be made active.
		    part.Transparency = 1;
	    else
		    part.Transparency = 0;
		end
    active = not active; -- Makes active the opposite value. If it's true then it becomes false, if it's false then it becomes true.
    end
end)

Hopefully I was of help. Have a nice day/night.

Thanks, Best regards, IdealistDeveloper

Hello, EmorchThe_Robloxian! The easiest way to do this is to use a remote event. To do this, do the following

Setup

  1. Create a remote event in ReplicatedStorage
    • Name the Event "KeyPressed"
  2. Create a script in ServerScriptService
    • Name the script "Receiver"
  3. Make a local script in StarterPlayerScripts
    • Name the script "Listener"

Script Making

Receiver Script:

event = game:GetService("ReplicatedStorage"):WaitForChild("KeyPressed")
part = INSTANCE of part to make appear --Make this define the part.  For example, "part = workspace.Model.Part"
 
event.onServerEvent:Connect(function(_)
    part.Transparency = 0
end)

Listener Local Script:

event = game:GetService("ReplicatedStorage"):WaitForChild("KeyPressed")
UIP = game:GetService("UserImputService")
 
UIP.ImputBegan:Connect(function(imput, typing)
    if typing == true then return end
    if imput.KeyCode == Enum.KeyCode.R then
	    event:FireServer()
	end
end)

Script Context

Above, you see a basic example of client-server communication with a remote event. This is a rundown of how it works. The client, or your computer, will detect when a button on your keyboard is pressed and if it is "R". If it is, it will fire something called a Remote Event. This "Remote Event", will take information from our client(You computer), and send it to the server (the game). In our case, once the server is fired, (Fired is a term used to represent a event being called/activated), it will make the part visible. Now, you may ask why not just make it visible on the client. The answer is because if you make it visible on the client, you will see it, but the server wont. This means that everyone else wont be able to see the brick, but you will.

With any further questions/comments/concerns, please comment or create a new question! ~mc3334

This discussion is preserved as it was. New answers, comments, and votes are closed.