Changing frame color

How to change Frame color when touching Part?
Please provide an example of a script.

Try this in a “Script”:

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.Touched:Connect(function(touched)
		wait(0.5)
		local GUI = plr.PlayerGui.ScreenGui.Frame --Frame/Gui
		local Color1 = Color3.new(163, 162, 165)
		if GUI then
			GUI.BackgroundColor3 = Color1
			print("Color has changed to "..Color1)
		else
			print("Frame has not been found")
		end
	end)
end)
1 Like

This fails to work with multiple people, because you’re connecting a new event every time a player joins.

@vts3g
Better solution:

local Players = game:GetService("Players")

local Part = script.Parent
local color = Color3.fromRGB(163, 162, 165)

Part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if player then
		local Frame = player.PlayerGui.ScreenGui.Frame -- Change reference if needed
		Frame.BackgroundColor3 = color
	end
end)
2 Likes

Should this be in a regular script?

Yes it should be in a regular script

1 Like

You could just use a local script and detect when the part was touched like this

workspace.Part.Touched:Connect(function(part)
   script.Parent.Parent.Frame.BackgroundColor3 = Color3.new(your color)
end)

I believe touched event can be used in local scripts

1 Like