make a script in lua roblox where if you press the proximity prompt in the button called…
- User
make a script in lua roblox where if you press the proximity prompt in the button called : GenButton ,every part in the folder called SiteLighting dissapears ,but if you press it again they reappear
Assistant
Certainly! Below is a Lua script for Roblox that achieves the described functionality. This script should be placed in a ServerScript inside the
GenButton.-- Get references to necessary objects local genButton = script.Parent -- Assuming the script is a child of GenButton local proximityPrompt = genButton:FindFirstChildOfClass("ProximityPrompt") local siteLightingFolder = workspace:FindFirstChild("SiteLighting") local parts = siteLightingFolder:GetChildren() local arePartsVisible = true -- Track the visibility state -- Function to toggle the visibility of parts local function togglePartsVisibility() arePartsVisible = not arePartsVisible for _, part in ipairs(parts) do part.Visibility = arePartsVisible and Enum.PartVisibility.Visible or Enum.PartVisibility.Hidden end end -- Function to handle the prompt triggered event local function onPromptTriggered(player) -- Toggle the visibility of the parts in SiteLighting togglePartsVisibility() end -- Connect the prompt triggered event to the handler function proximityPrompt.Triggered:Connect(onPromptTriggered)Explanation:
-
References:
genButton: References theGenButtonassuming the script is a child ofGenButton.proximityPrompt: Finds theProximityPromptwithinGenButton.siteLightingFolder: Finds theSiteLightingfolder in the workspace.parts: Gets all the children (parts) inside theSiteLightingfolder.arePartsVisible: A boolean to track if the parts are currently visible.
-
togglePartsVisibility Function:
- Toggles the
arePartsVisibleboolean. - Iterates through each part in the
partstable. - Sets the
Visibilityproperty of each part toVisibleifarePartsVisibleistrue, otherwise sets it toHidden
Note: Roblox parts do not have a
Visibilityproperty. Instead you should use theTransparencyproperty. If you want to hide the parts you should set theTransparencyto 1 and to show them set it to 0. Here is the corrected code:-- Function to toggle the visibility of parts local function togglePartsVisibility() arePartsVisible = not arePartsVisible for _, part in ipairs(parts) do part.Transparency = arePartsVisible and 0 or 1 end end - Toggles the
-
onPromptTriggered Function:
- This function is called whenever the
ProximityPromptis triggered. - It calls
togglePartsVisibilityto toggle the visibility of the parts.
- This function is called whenever the
-
Event Connection:
- Connects the
Triggeredevent of theProximityPromptto theonPromptTriggeredfunction.
- Connects the
Notes:
- ProximityPrompt:
- Ensure that the
GenButtonhas aProximityPromptas a child.
- Ensure that the
- SiteLighting Folder:
- Ensure that there is a folder named
SiteLightingin the workspace and it contains the parts you want to toggle.
- Ensure that there is a folder named
- Running on the Server:
- This script should be a ServerScript because it manipulates game objects which should be handled on the server to prevent exploitation.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.