How long should I wait before continuing script after forcing player to stop sitting for it to properly function?

I’m trying to make a “Hide” system where you can hide under furniture. For this to work the player cannot be sitting in a seat so when they hide I make them stand up. When I wait 1 heartbeat it doesn’t register in time and script continues with the player still sitting. How long should I wait so that the player stands up and the script can properly move their character to the hiding location?

local Head = player.Character.Head
local cframe = player.Character.HumanoidRootPart.CFrame
player.Character.Humanoid.Sit = false
game:GetService("RunService").Heartbeat:Wait()
player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(part.Parent.Hide.CFrame.X,part.Parent.Hide.CFrame.Y-1.5, part.Parent.Hide.CFrame.Z))
player.Character.HumanoidRootPart.Anchored = true
player.Character.Humanoid.WalkSpeed = 0
player.CameraMode = Enum.CameraMode.LockFirstPerson
HideRemote:FireClient(player, part.Parent.Exit, cframe)

Can you show the script, please

I updated the post. I should have originally included it, sorry.

Have you tried printing before hertbearting and after hertbearting. Maybe that’s the problem of some next lines

When the Character is sitting down, its HumanoidStateType would be set to Enum.HumanoidStateType.Seated. As soon as the Character leaves the seat and is jumping, swimming, running, etc. it’ll be updated to a different value other than Seated.

With that in mind, after you update the Humanoid.Sit property, the Script could be modified to wait until their HumanoidStateType is updated to a value that is not equal to Seated. You could use the Humanoid.StateChanged event to detect when it changes.

  • This would be able to precisely detect when the player is no longer seated (which is important, since the amount of time it’d take to unseat the Character model could vary depending on other things that are happening in the game at that moment) so that the Script teleports the Character as soon as it can without doing it too early, ensuring that the seat is not teleported with the Character model.

Here’s an example revision:

Example

-- Other necessary variables above
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid", 5)
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart", 5)

if not Humanoid then return end
if not HumanoidRootPart then return end

local currentState = Humanoid:GetState()


if Humanoid.Sit == true or currentState == Enum.HumanoidStateType.Seated then

    local allowedToTeleportPlayer = false

    Humanoid.StateChanged:Connect(function(newState)
        if newState ~= Enum.HumanoidStateType.Seated then
            allowedToTeleportPlayer = true
        end
    end)

    Humanoid.Sit = false
    -- You can also make use of Humanoid:ChangeState() to unseat the player

    if Humanoid:GetState() == Enum.HumanoidStateType.Seated then
        repeat task.wait() until allowedToTeleportPlayer == true
    end
end

task.defer(function()
    local targetLocation = CFrame.new(0, 0, 0)
    -- Update this to the CFrame that the Character should be teleported to

    Character:PivotTo(targetLocation)
    -- Continue with anchoring HumanoidRootPart, setting WalkSpeed, etc.
end)

Additional Resources

Humanoid:GetState() - Roblox Creator Hub Documentation page

Model:PivotTo() - Roblox Creator Hub Documentation page

task.defer() - Roblox Creator Hub Documentation page

The player is not always seated, but can be so I used this line of code instead. Thanks for the help.

repeat task.wait() until player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Seated
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.