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

How to make a brick, that when you touch it, it changes text/transparency?

So here's the story, whenever a character touches a brick, I want the gui to change text/transparency, however I do not know what is wrong with my script.

script.Parent.Touched:Connect(function()
	game.StarterGui.ScreenGui.Frame.TextLabel.Text = "hellloooo"
end)

The script in the brick ^^^

1 comment

0

A copy of all the stuff in StarterGui is copied into each player's PlayerGui container when they spawn. With this script, you're changing the base copy of the GUI, and not the GUI the player currently has.

2 answers

✓ Accepted answer

Hello.

The reason as to why you aren't seeing changes to the Frame text, is because you are changing the frame in the StarterGui, instead of the PlayerGui. The StarterGui is different compared to the PlayerGui for a very important reason. The StarterGui, as said in it's name, is what gives the player the Guis when they join, and the PlayerGui, also said in it's name, is the Guis present in the Player.

To make changes to the gui, you are required to change the gui from the PlayerGui instead. So with your script, assuming it's a serverscript, you need to get the player via the Touched function, simply done with a Players:GetPlayerFromCharacter(hit.Parent). We tend to use a hit function for the touched function, as it's used to define the player.

Now let's take your script and apply these changes:

local Players = game:GetService("Players") -- Get the players
local Part = script.Parent -- Gets the part that needs to be touched

Part.Touched:Connect(function(hit) -- Set the hit function
	local Player = Players:GetPlayerFromCharacter(hit.Parent) -- Get the LocalPlayer via the touched event
	local PlayerGui = Player:WaitForChild("PlayerGui") -- Get the PlayerGui
	PlayerGui.ScreenGui.Frame.TextLabel.Text = "hellloooo" -- Set the text from the gui in PlayerGui
end)

Hope this helps!

No, you also have to make sure if a player touched the part:

local Part = script.Parent

Part.Touched:Connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if hum then -- Do whatever the person on top of me did

end)

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