Hello DevForum! I need help on creating a chair that turns when you sit on it.
I have two parts, the seat, and the base. Both the seat and the base have attachments and the base has an AlignPosition
object in it connected to the attachment in the seat and in the base in order to make it free in movement. I also have made a script inside the seat to create other attachments for the seat to turn to:
--[[Script:SeatScript]]--
--Variables--
local players = game:GetService("Players")
local seat = script.Parent
local base = seat.Parent.Base
--Seat Position
seatVect = {
x = seat.Position.X,
y = seat.Position.Y,
z = seat.Position.Z
}
--Seat Rotation
seatRot = {
x = seat.Orientation.X,
y = seat.Orientation.Y,
z = seat.Orientation.Z
}
--Seat Size
seatSize = {
x = seat.Size.X,
y = seat.Size.Y,
z = seat.Size.Z
}
--Front Attachment--
local FrontAtt = Instance.new("Attachment")
FrontAtt.Name = "FrontAtt"
FrontAtt.Parent = base
FrontAtt.WorldPosition = Vector3.new(seatVect.x, seatVect.y, (seatVect.z + (seatSize.z * 1) + 0))
--Back Attachment--
local BackAtt = Instance.new("Attachment")
BackAtt.Name = "BackAtt"
BackAtt.Parent = base
BackAtt.WorldPosition = Vector3.new(seatVect.x, seatVect.y, (seatVect.z + (seatSize.z * -1) + 0))
--Misc Functions--
local function DrawBeam(Parent, Att0, Att1, Name)
local Beam = Instance.new("Beam")
Beam.Parent = Parent
Beam.Enabled = true
Beam.Attachment0 = Att0
Beam.Attachment1 = Att1
if Name then
Beam.Name = Name
end
end
--Beam-- (Used for testing the position of attachments FrontAtt and BackAtt
if FrontAtt and BackAtt then
DrawBeam(FrontAtt, FrontAtt, BackAtt)
end
----------------------
local currentPlayer = nil
--Main Functions--
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local humanoid = seat.Occupant
if humanoid then
local character = humanoid.Parent
local player = players:GetPlayerFromCharacter(character)
if player then
--print("sitting")
currentPlayer = player
return
end
end
if currentPlayer then
--print("getting up")
currentPlayer = nil
end
end)
The seat works, but I don’t know how to make the seat point towards the attachments and that because im using an AlignPosition
object, which makes the seat seem like its ‘hovering’, this happens:
robloxapp-20200515-1122157.wmv (536.7 KB)
It rotates freely on the x and y rotation axis’. I don’t really want it to do that, but it happens anyway.
Reason why im using AlignPosition
is because I tried using the hinge constraint, and it couldnt turn left or right freely. Plus, im not sure if there is a way to restraint an object from moving up or down without using a script that when the object moves, it makes it stay at the exact rotation values.