Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a GUI change color when you touch a brick?

Asked by 3 years ago

Hey there Devs I wanted to ask that how can I make a GUI change its color when a player touches a part for example : My GUI Color is red and when I touch a part it turns blue. I have been watching youtube for soo long asking the same question. I hope someone can help me if you didnt understand quite well what i said just ask me

3 answers

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

Note that this ain't a request site, you have to try to solve your problem first before you ask here. However since your new I'll help you out this once.

In the gui that you want to change color, insert a local script with the following script:

game.Workspace:WaitForChild("ColorPart",10) -- waits for a part in workspace called ColorPart and if it doesn't appear in 10 seconds, it will stop waiting and continue with the script. (Make sure there actually is a part in workspace called ColorPart.

--Variables (to shorten the script a bit and make it easier)
local Part = game.Workspace.ColorPart
local plr = game.Players.LocalPlayer -- NOTE: LOCALPLAYER ONLY WORKS ON LOCAL SCRIPTS

Part.Touched:Connect(function(hit) --fires a function when ColorPart is Touched by something
    if hit.Parent.Name = plr.Name then -- checks if the thing that touched's name is your username to confirm it is you who touched it
        local something = script.Parent
        something.BackgroundColor3 = Color3.fromRGB(0,127,255) -- this changes the gui's color to azure color, mess around with the rgb scale all you want. If you want to use the HSV scale instead then change fromRGB to fromHSV.
    end
end)
0
-1. "This ain't a request site" yet you proceed to spoonfeed. DeceptiveCaster 3761 — 3y
Ad
Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

The youtube tutorials are outdated. We use PlayerGui for now. Accessing PlayerGui needs LocalPlayer so you need to use LocalScript.

Example:

local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui").Frame

game.Workspace.Part.Touched:Connect(function()
    gui.BackGroundColor3 = Color3.new(0,0,255)
end)
0
NOTE again: make this a localscript, and then locate the GUI you want in the first line starting from the dot next to PlayerGui Xapelize 2658 — 3y
0
if you dont know, ask me here, ill help you more Xapelize 2658 — 3y
Log in to vote
0
Answered by
iactz 15
3 years ago

Assuming you want it to change locally, here is how I would go about scripting it.

First off I would define every variable we need.


local part = script.Parent -- if script isn't being put in part change this to the path of the part local plr = game:GetService("Players").LocalPlayer -- only works in a local script local plrgui = plr:WaitForChild("PlayerGui") local gui = -- path to gui, can be started using plrgui then probably followed by a screengui and frame of some kind

Second, we can use the part.touched function to get when a part is hit,

part.Touched:Connect(function(hit --[[hit is equal to what is touching it when this is ran]])
    -- everytime this part is touched, the code inside this will be ran
end)

since you want it to happen everytime a player is hit we have to make sure the code is only being ran when hit is a player

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        --[[now any code here wont be ran unless "hit" has a humanoid in its parent like a player model]]
    end
end)

Now that we have the player that hit it, we can run the code to change the color of a gui.

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        gui.BackgroundColor3 = Color3.new(0,255,0) --sets color to blue
    end
end)

That's all, at the end your code should look something like this

local part = script.Parent -- if script isn't being put in part change this to the path of the part
local plr = game:GetService("Players").LocalPlayer -- only works in a local script
local plrgui = plr:WaitForChild("PlayerGui")
local gui = -- path to gui, can be started using plrgui then probably followed by a screengui and frame of some kind

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        gui.BackgroundColor3 = Color3.new(0,255,0) --sets color to blue
    end
end)

Hope this helped you!!! If you don't understand anything add me on discord (actz#2196) and I can go over it with you.

0
Thank you soo much this helped me alot kidsteve923 139 — 3y

Answer this question