When part touched part turns green

I am trying to make this part turn green when a player touches it:
slika_2022-07-13_114442584

yourPart.Touched:Connect(function(partThatTouched)
    --Check if whatever touched is a player
    if game.Players:GetPlayerFromCharacter(partThatTouched.Parent) then
        --Change color
    end
end)
2 Likes

There are many tutorials on .Touched you simply just have to look.

local Part = script.Parent

Part.Touched:Connect(function()
   Part.Color = Color3.fromRGB(0,255,0)
end

Try doing

script.Parent.Touched:Connect(function()
script.Parent.Color = Color3.fromRGB() --color
end)
script.Parent.Touched:Connect(function()
    script.Parent.Color = Color3.fromRGB(0,255,0)
end)

This would make the block green after touched.

While this does work, it’s actually possible for .Touched to detect accessories instead of body parts. So to solve this, we can use :FindFirstAncestorWhichIsA().

local PS = game:GetService("Players")

local part -- insert your part here

part.Touched:Connect(function(toucher)
	local model = toucher:FindFirstAncestorWhichIsA("Model")
	local player = model and PS:GetPlayerFromCharacter(model)
	
	if player then
		-- insert color changing code
	end
end)
1 Like