so im trying to make a gui appear
here’s the script
local sg = game:GetService("StarterGui")
local part = game.Workspace.Part
part.Touched:Connect(function(click)
if click then
sg.ScreenGui.TextButton = true
end
end)
There is a lot wrong with this script. Beneath this is an improved version, I have added comments to explain what everything means.
script.Parent.Touched:Connect(function(touch) -- onTouch
if touch.Parent:FindFirstChild("Humanoid") then -- If we find a Humanoid (all players have a humanoid)
local Player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent) -- Define the player
local PlayerGUI = Player.PlayerGui -- Find GUIs in the PlayerGUI instead of StarterGUI
local GUI = PlayerGUI:WaitForChild("ScreenGUI") -- ScreenGUI that you want
GUI.TextButton.Visible = true -- Make it visible
end
end)
ScreenGui gets cloned to the player when the game starts, so you have to get it from PlayerGui inside the player
You can remove this line:
local sg = game:GetService("StarterGui")
You are currently using part.Touched which check if something touches the part. If you want the player to be able to click it instead, you would need to insert a ClickDetector inside the part and chage the function to clickDetector.MouseClick. This give you the player who clicked it, example:
local part = game.Workspace.Part
local clickDetector = part .ClickDetector
clickDetector.MouseClick:Connect(function(player)
print(player.Name)
end)
If you want to check when the player touches the part (this gives the part that touched), example:
local part = game.Workspace.Part
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) -- Define the player
print(player.Name)
end
end)
Now you want to make the TextButton visible, which can be done like this in either of the mentioned functions (I also recommend reading the comments in the code @Lostude wrote, and reading thoroughly through @JuanGamerPlayz_RBLX script to try and understand what is going on for use in future scripts):
local PlayerGUI = player.PlayerGui
local GUI = PlayerGUI:WaitForChild("ScreenGUI")
GUI.TextButton.Visible = true
Asking this as you might want to make an instance for the class… If you have already, then you can now basically create a script with the following:
local Part = game.Workspace.Part
local Touched_The_Part = false -- A little debounce to indicate once getting the part touched.
Part.Touched:Connect(function(player)
Touched_The_Part = true
local Player = game.Players.LocalPlayer -- Variable in order to get the player property for the next steps below.
local PlayerGui = Player.PlayerGui -- Property where the interfaces are located as the class StarterGui simply clones and it is not the primary parent.
local The_Gui = PlayerGui.ScreenGui -- Variable to locate the GUI you created (this is a sample and you can rename it).
local Button = The_Gui.TheButton
if Touched_The_Part == true then
Button.Visible = true -- This is going to make the button visible once touching the following part.
end
end)
Everything being said above is the method of how you can do it by using the event .Touched! It is possible to also do this by ClickDetector.MouseClick and ProximityPrompt.Triggered while always creating a parameter to get the player.