I have a game where you can build your own restaurant and guests can come and go and interact with their surroundings. However, I keep having loads of issues with getting them to sit down in chairs and on toilets.
When a guest stops sitting on a seat it will teleport via CFraming to an invisible part, GuestTargetPoint, in front of it which is the position the guests pathfind to. However, when they are CFramed, they also bring the seat with them and I don’t know why. The guests do not sit using Seat:Sit() because when I did this before, guests were unable to stop sitting even when using ChangeState, setting sit to false or jumping. They would be standing up but the humanoid still thinks they are sitting down and they cannot move anymore.
Here you can see the seat has moved:
The part on the left is the part they pathfind to (GuestTargetPoint), the part on the right behind the NPC is the actual seat. What happened was a guest got off the toilet on the left and sat on the one on the right, but it took the seat with it. This also breaks the game in other ways as the guests will pathfind to an occupied toilet and sit inside other guests because there are technically two seats in one position.
Correct positioning:
So I have decided I want to find out how to properly make my guests sit down without anchoring them and playing the sit animation.
My question is this: How can I get my NPCs to sit down on seats properly, without breaking themselves or taking the seat part with them?
Current code for sitting on toilets:
local toilet = SearchForItem(parentPlot, 'Toilet', humanoid.RootPart, true)
if toilet and not toilet.Occupied.Value and toilet:FindFirstChild('GuestTargetPoint') then
local yield = PathfindNPC(body, humanoid, toilet.GuestTargetPoint.Position + Vector3.new(0, 0.5, 0))
if yield and toilet and not toilet.Occupied.Value and
(humanoid.RootPart.Position - toilet.GuestTargetPoint.Position).magnitude < 9 then
toilet.Occupied.Value = true
if toilet:IsA('Seat') then
humanoid.RootPart.CFrame = toilet.CFrame + Vector3.new(0, humanoid.HipHeight, 0)
humanoid.Sit = true
body.Head.Anchored = true
wait(math.random(8, 12))
toilet.Occupied.Value = false
body.Head.Anchored = false
humanoid.Sit = false
humanoid:MoveTo(toilet.GuestTargetPoint.Position)
else
wait(math.random(8, 12))
toilet.Occupied.Value = false
end
else
print('Too far! ' .. (humanoid.RootPart.Position - toilet.GuestTargetPoint.Position).magnitude)
end
end
SearchForItem will return an object with the CollectionService tag ‘Toilet’, and PathFind is the function that makes the guests walk to the target. Occuiped is a Boolean that determines if there is a guest on the seat. There are two toilet classes in the game but the problem is with the seat type, seat:IsA(‘Seat’)
Thanks in advance!