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

How can I make this ClickDetector Script Local Only?

Asked by
AAzterr 27
5 years ago
Edited 5 years ago

Okay so, I have this section in an obby where the player has to click on a part to "build" them. The problem is, this script works globally so it's happening for all players, so how could I have it only pop up for the player who clicked on it? Inside that brick there is a ClickDetector and inside the ClickDetector resides this script.
My Script (Class is Script, not LocalScript):

function BuildBrick()
    script.Parent.Parent.CanCollide = true
    script.Parent.Parent.Transparency = 0

end


script.Parent.MouseClick:Connect(BuildBrick)

0
don't forget to accept my answer if it solves your problem. User#24403 69 — 5y
0
It should be under the answer. User#24403 69 — 5y

1 answer

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

It is possible to use a local script to listen to the mouse click event, as long as the local script is in a place they can run in, generally PlayerScripts. The only thing you'll need to change is script.Parent to the path of your click detector.

local clickDetector = ... -- # change to your clickdetector path

local function BuildBrick(client) -- # localise variable
    if client == game:GetService("Players").LocalPlayer then
        clickDetector.Parent.CanCollide, clickDetector.Parent.Transparency = true, 0
    end
end

clickDetector.MouseClick:Connect(BuildBrick)

Notice the additional check. That is because MouseClick can fire even if it was not the local player! The code in the if will not execute if the local player does not click the part.

0
Could you help me out with that answer I posted? Or should I just respect it? Ziffixture 6913 — 5y
0
It worked! Really big thanks! AAzterr 27 — 5y
0
Also, how do I accept an answer? AAzterr 27 — 5y
0
You can't accept answers at 0 reputation.. Keep that in mind, incapaxx Miniller 562 — 5y
View all comments (3 more)
0
wrong User#24403 69 — 5y
1
no prob for the accept xd RubenKan 3615 — 5y
0
xddd User#24403 69 — 5y
Ad

Answer this question