I am making a barrier gate to go with my military game, as shown in the photo below. The problem is that I don’t know how to tween/move the part? I tried tweening (script below) and adding hinges/angular velocity to make it move. Nothing makes it moves, except for the hinge but it’s not going up.
How would I make the part move when a player/car enters an invisible part?
local TweenService = game:GetService("TweenService")
script.Parent.Touched:Connect(function(hit)
if hit:IsA("Model") then
if hit.Parent:FindFirstChild("VehicleSeat") then
local player = hit.Parent.VehicleSeat.Occupant
if player.Name == "LMVM2041" then
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local tween = TweenService:Create(game.Workspace:FindFirstChild("B2").TweenThis, tweenInfo, {Position = CFrame.new(228.015, 29.718, 915.709)})
tween:Play()
end
end
else
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local tween = TweenService:Create(game.Workspace:FindFirstChild("B2").TweenThis, tweenInfo, {Position = CFrame.new(228.015, 29.718, 915.709)})
tween:Play()
end
end
end)
You have the situation where you want it to rotate by the end of the gate rather than the middle of it. This is what I prefer to do:
Step 1
Create an invisible part that is the exact same size as the barrier part EXCEPT make it twice as long. Position it like shown below (tried my best to show it):
Step 2
Weld the ACTUAL part to the invisible part. This is because we will be rotating the invisible part instead of the actual part, but since it’ll be welded, the actual part will move with the invisible part. Make sure the invisible part has Anchored set to TRUE and CanCollide set to FALSE while the actual barrier has Anchored set to FALSE and CanCollide set to TRUE. If you don’t know how to weld, here’s a video below that goes into it (it’s really easy).
Step 3
Rotate the invisible part through TweenService! Once you’ve welded the barrier to the invisible part, you could do it like this:
local TS = game:GetService("TweenService")
local info = TweenInfo.new(
2,
Enum.EasingStyle.Quad,
Enum.EasingStyle.Out,
0,
false,
0
)
local rotateUp = TS:Create(
invisiblePart, -- your invisible part here
info,
{CFrame = invisiblePart.CFrame * CFrame.Angles(math.pi / 2, 0, 0)}
)
rotateUp:Play() -- May need to tweak the CFrame.Angles, but it'll rotate 90 degrees
Nope. Vector3 only controls a part’s position. It also controls a part’s orientation, but nobody really uses the orientation property when rotating something. CFrame is the way to go.
CFrame is basically position + rotation. So we use that here.
local TS = game:GetService("TweenService")
local db = false
local info = TweenInfo.new(
2,
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out,
0,
false,
0
)
local rotateUp = TS:Create(
game.Workspace.BaseBarrier.InvisiblePart,
info,
{CFrame = game.Workspace.BaseBarrier.InvisiblePart.CFrame * CFrame.Angles(math.pi / 2, 0, 0)}
)
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if db == false then
db = true
rotateUp:Play()
game.Workspace.BaseBarrier.Model.EmergencyLight.Toggle.Value = true
print("played")
rotateUp.Completed:Wait()
print("Done")
game.Workspace.BaseBarrier.Model.EmergencyLight.Toggle.Value = false
db = false
end
end
end)
This just means you gotta change the CFrame.Angles a little.
I believe I had it set to CFrame.Angles(math.pi / 2, 0, 0).
Perhaps try CFrame.Angles(0, 0, math.pi / 2)? Try putting the math.pi / 2 in different areas while leaving the rest as 0 until it’s the one you want. You can also make it negative if it’s rotating in the opposite direction it should.
Probably the last thing, it works when I put it in the z value, but it goes down instead of up. I tried doing math.pi*2 but it doesn’t do anything. I’m also assuming math.pi/-2 will not work because that would be unlikely?