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
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)
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)
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.
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)
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)
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.
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.