Change color of part on touch

i just started learning lua and after reading a few pages i thought it was time to test what i learned, tho i dont know what i did wrong, its supposed to change the color of the part when you touch it but it doesnt.

local part = script.Parent
local pink = Color3{253, 20, 242}

part.Touched:Connect(OnTouch)
function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		part.Color = pink
		end
end

2 Likes

Put the connection

part.Touched:Connect(OnTouch)

after the function’s variable

function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		part.Color = pink
		end
end

The result would be as follows

local part = script.Parent
local pink = Color3{253, 20, 242}

function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		part.Color = pink
		end
end
part.Touched:Connect(OnTouch)
2 Likes
local part = script.Parent
local pink = Color3.new(253, 20, 242)


function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		part.Color = pink
		end
end
part.Touched:Connect(OnTouch)

Try this

1 Like

You’ll need to change your 2nd line of code. With Color3, you actually need to add .new, so that should be:

local pink = Color3.new(253, 20, 242)

Also, make sure you’re using regular parenthesis instead of curly brackets!

Otherwise, your function looks good. Make sure to fix those indentations, though! It’s a good habit to keep your indents in order so you can debug your code easier.

1 Like

I added what everyone suggested, but this turns the part green instead of the desired testing pink, i got the RGB code from the color code from the properties of a part i made pink.

local part = script.Parent
local pink = Color3.new(253, 20, 242)

function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		part.Color = pink
	end
end
part.Touched:Connect(OnTouch)

I just tested your RGB values, they do give a pink color. Make sure you copy/pasted them correctly into your script.

Edit: This is what those three RBG values will show:

Your 2nd line of code should be exactly:

local pink = Color3.new(253, 20, 242)

I would recommend copying and pasting the line above directly into your script so you can make sure you don’t have any errors.

1 Like
local Part = script.Parent
local Pink = Color3.fromRGB(253, 20, 242)

local function OnTouch(Hit)
    if Hit.Parent:FindFirstChildOfClass("Humanoid") then
        Part.Color = Pink
        print("Set.")
    end
end

Part.Touched:Connect(OnTouch)

This will work as you’d like, enjoy! :smiley:

2 Likes

Incorrect. Color3.new() doesn’t return correct in accordance with RGB color components. Make sure to use Color3.fromRGB!

1 Like

thank you all for your help! It worked.

1 Like

Ah okay, my bad! I don’t do a lot with colors. Now that I’m looking in the API, Color3.new() does work with RBG but you have to use the values between 0 and 1 instead of out of 255.

2 Likes
local Players = game:GetService("Players")

local Part = script.Parent

local function OnTouch(Hit)
	local HitModel = Hit:FindFirstAncestorOfClass("Model")
	if HitModel then
		local HitPlayer = Players:GetPlayerFromCharacter(HitModel)
		if HitPlayer then
			Part.Color = Color3.new(1, 0, 1) --Pink.
		end
	end
end

Part.Touched:Connect(OnTouch)

You should write the script like this, such that the player’s character’s tools/accessories will trigger the correct response from the “.Touched” event as well.

1 Like