Want to stay connected to the Scripting Helpers community and get real-time help? Join the Scripting Helpers Discord Server!
Log in to vote

How do you use tools to change a part's color when clicked?

Asked by 8 days ago

I have made a click detector and coded a script that will change the part's color when it is clicked but now I want to make it only turn that color when the player clicks on the brick with a certain tool.

clickDec = script.Parent.ClickDetector
part = script.Parent

    if  player.Character.PartClicker and then
        part.BrickColor = BrickColor.new("Really Red")

The MouseClick event doesn't even get called when they are holding the tool and it shows I can click on the part when I'm not holding a tool but when I am it doesn't.

Does anyone know how to make it so the tool can execute an event when it clicks on the part?

you can use Rays instead of click detectors Is_Hunter 23 — 8d

1 answer

Log in to vote
Answered by 8 days ago
Edited 8 days ago

You can use Rays to do it

-- this script is inside the tool
local Tool = script.Parent
local Players = game:GetService"Players"
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Distance = 300 -- a distance from where you can change the part's color
local BrickColor = BrickColor.new("Really red") -- wich color you want to apply on the part

local function onActivation()
    local Ray = Ray.new(tool.Handle.CFrame.p, (Mouse.Hit.p - Tool.Handle.CFrame.p).unit * Distance)
    local Part, Position = workspace:FindPartOnRay(Ray, Character, false, true)
    Part.BrickColor = BrickColor


I hope it helps.

Thanks for the help! It worked! Adv3rtizement 63 — 8d
Your Welcome^^ Is_Hunter 23 — 8d

Answer this question