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

How do I detect when a player stops sitting?

Asked by
Validark 1580 Snack Break Moderation Voter
10 years ago

So I have this script:

seat = script.Parent
function c(Player)
Player.Character.Humanoid.Sit = true
Player.Character.Torso.CFrame = CFrame.new(seat.Position + Vector3.new(0,seat.Size.X / 2.4,0))
Player.Character.Torso.CFrame = Player.Character.Torso.CFrame * CFrame.Angles(0,3.1416 / 2,0)
end -- End
seat.clickdetector.MouseClick:connect(c)

I really like it that you can click on it to sit rather than jump on it 4 times hoping you will sit but I want to have some sort of safeguard so that you can't keep clicking it while you are on the seat. I want to make it so that when you click it to sit down the Clickdetector range gets set to 0, but is restored once the player gets off.

I have thought about using ChildAdded or ChildRemoved to connect but it doesn't create a Seatweld. Is there something I am overlooking?

2 answers

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

When the player clicks on the block, the Sit property of it's Humanoid changes to true. As soon as the player jumps to exit the seat, the Sit property changes back to false. So if you simply add in the line repeat wait() until not Player.Character.Humanoid.Sit we can determine when the player is no longer sitting in the seat. (You could also use the Changed event in order to detect when the property Sit changes, which ever you prefer.)

seat = script.Parent
function c(Player)
    Player.Character.Humanoid.Sit = true
    Player.Character.Torso.Anchored = true
    Player.Character.Torso.CFrame = CFrame.new(seat.Position + Vector3.new(0, (seat.Size.Y/2) + 1, 0))
    Player.Character.Torso.CFrame = Player.Character.Torso.CFrame * CFrame.Angles(0,3.1416 / 2,0)
    seat.ClickDetector.MaxActivationDistance = 0
    repeat wait() until not Player.Character.Humanoid.Sit
    Player.Character.Torso.Anchored = false
    wait(1)
    seat.ClickDetector.MaxActivationDistance = 32
end
seat.ClickDetector.MouseClick:connect(c)

Question Irrelevant Thing: While testing, I noticed that when you walk and click at the same time, you fall off the chair as momentum is carried into the positioning of the Character. I modified it slightly by making it so your character is anchored while sitting and unanchored as soon as it exits, preventing the previously mentioned problem.

Ad
Log in to vote
1
Answered by 10 years ago
seat = script.Parent
function c(Player)
if Player.Character.Humanoid.Sit == true then return end -- This way, when you jump, ROBLOX autosets your sit to false. If ROBLOX has not registered that you're up then it will not let you click again.
Player.Character.Humanoid.Sit = true
Player.Character.Torso.CFrame = CFrame.new(seat.Position + Vector3.new(0,seat.Size.X / 2.4,0))
Player.Character.Torso.CFrame = Player.Character.Torso.CFrame * CFrame.Angles(0,3.1416 / 2,0)
end -- End
seat.clickdetector.MouseClick:connect(c)

Answer this question