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

I was wondering if i can make a seat that when you sit in it a click detector appears on a part?

Asked by 4 years ago

i have the click to earn 1+ leaderstats already but i want to have it where if you sit you can only click it so when you get up from the seat you cant click it anymore

1 answer

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

With the humanoid.StateChanged Event (Humanoid is the "core" part of a player.) you can detect whenever a player is sitting, walking, etc. Click detectors have a property called MaxActivationDistance which sets how far a player can click the Click Detector. If we set the MaxActivationDistance to 0 when the player is not sitting the player won't be able to click the Click Detector. Below is an example of what you could do:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid", 10)

local clickDetector = game.Workspace.Button.ClickDetector

humanoid.StateChanged:Connect(function(oldState, newState)

    --Setting the Click Detector to a clickable distance when the player is sitting.
    if newState == Enum.HumanoidStateType.Seated then
        clickDetector.MaxActivationDistance = 20
    end

    --Setting the Click Detector to an unclickable distance when the player is not sitting.
    if newState ~= Enum.HumanoidStateType.Seated then
        clickDetector.MaxActivationDistance = 0
    end

end)

clickDetector.MouseClick:Connect(function()
    print("Clicked!")
    --Whatever code you want to activate when the Click Detector is clicked
end)
Ad

Answer this question