I am trying to change the colour of a part with a certain name if a player touches it, but idk what does not work in my script.
local player = game:GetService("Players").LocalPlayer
player.Touched:Connect(function(part)
if part.Name == "Grass" then
part.Brickcolor = BrickColor.new("Black")
else
print("oof")
end
end)
game.Workspace.MyPart.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
if MyPart.Name == "Grass" then
MyPart.Brickcolor = BrickColor.new("Black")
end
end
end)
That won’t make any difference, the reason why the script does not work is because you are using LocalPlayer which is a model not a part. Instead use a certain part of the character.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
for _, part in pairs(character:GetChildren()) do
if part:IsA("BasePart") then
part.Touched:Connect(function(part) -- add a debounce
if part.Name == "Grass" then
part.Brickcolor = BrickColor.new("Black")
else
print("oof")
end
end)
end
end
or what @Valkyrop did which is the obvious thing to do.