Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years 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

clickDec.MouseClick:connect(function(player)
    if  player.Character.PartClicker and then
        part.BrickColor = BrickColor.new("Really Red")
    end
end)

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?

0
you can use Rays instead of click detectors Is_Hunter 152 — 5y

1 answer

Log in to vote
0
Answered by
Is_Hunter 152
5 years ago
Edited 5 years ago

You can use Rays to do it

-- this script is inside the tool
-- LOCALSCRIPT
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
end

Tool.Activated:connect(onActivation)

I hope it helps.

0
Thanks for the help! It worked! Adv3rtizement 101 — 5y
0
Your Welcome^^ Is_Hunter 152 — 5y
Ad

Answer this question