I have a script that should gradually speed up a model’s movement up to 1, then slowing it down back to 0 when the set key is released.
But, with what I did, it isn’t working properly.
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local SpotlightHead = game.Workspace.Spotlight.Arm.Head
local UpdateCon
local newCFame = SpotlightHead.PrimaryPart.CFrame
local goalStart = {}
newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(1, 0, 0)
local goalEnd = {}
newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(0, 0, 0)
local tweenInfo = TweenInfo.new(
0.5,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
-1,
true,
0
)
local tweenStart = TweenService:Create(newCFrame, tweenInfo, goalStart)
local tweenEnd = TweenService:Create(newCFrame, tweenInfo, goalEnd)
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.KeypadEight then return end
if UpdateCon then
UpdateCon:Disconnect()
UpdateCon = nil
end
UpdateCon = RunService.Heartbeat:Connect(function(dt)
tweenStart:Play()
SpotlightHead:SetPrimaryPartCFrame(newCFrame)
end)
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.KeypadEight then return end
if not UpdateCon then return end
tweenEnd:Play()
UpdateCon:Disconnect()
UpdateCon = nil
end)
the first argument is the target object where it will tween a property of that object
the second is the tween info
the third is a dictionary of the goal values
also tweening just a CFrame wont do anything
you should tween the CFrame of the object you want to move
imma quit trying to sound professional, not that good at this lol
my goal was to make it so a model would move gradually up to what the goalStart value is, much like a car accelerating, then the goalEnd value is when i release the key im pressing, so the model keeps moving, but slows down back to 0, like a car brake.
and im not sure what to use as the instance honestly, again, not the smartest with scripting, just trying to help someone out
the primary part of the car should be the instance that you are tweening
move the cframe of the primary part of the car with something like quad or exponential tweening
in my code i made it stop after some studs by tweening it in the direction its looking
tweak the studs forward and time amount to get it right with the speed that the car is going
you can also make them change based on the speed of the car to make it correct for different speeds
local ts = game:GetService("TweenService")
local studsFoward = 5 -- studs traveled forward at end of brake tween
local tweenInfo = TweenInfo.new(0.4, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
local carObject = carHere -- put the car model here
local primaryPart = carObject.PrimaryPart
--also remember to set the .PrimaryPart property of the car (manually)
--the primary part should be the base or most important part of the car that represents its position
local brakeTween = ts:Create(primaryPart, tweenInfo, {
CFrame = primaryPart.CFrame + CFrame.new(primaryPart.CFrame.LookVector*studsFoward)
})
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local SpotlightHead = game.Workspace.Spotlight.Arm.Head
local UpdateCon
local newCFrame = SpotlightHead.PrimaryPart
local goalStart = {}
newCFrame.CFrame = CFrame.new(1, 0, 0)
local goalEnd = {}
newCFrame.CFrame = CFrame.new(0, 0, 0)
also changing the cframe and then changing it immediately after will just change it to the last one you changed it to
If you want to make a start and a goal then set the CFrame of the spotlight to the start CFrame and then the CFrame in the tween will be the goal CFrame
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local SpotlightHead = game.Workspace.Spotlight.Arm.Head
local UpdateCon
local spotLight= SpotlightHead.PrimaryPart
local goalStart = {}
spotLight.CFrame = CFrame.new(1, 0, 0)
local goalEnd = {}
spotLight.CFrame = CFrame.new(0, 0, 0)
tweaked things around a bit, now it shows an error.
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local SpotlightHead = game.Workspace.Spotlight.Arm.Head
local UpdateCon
local SpotlightPOS = SpotlightHead.primaryPart
SpotlightPOS.Position = CFrame.Angles(0, 0, 0)
local moveStart = SpotlightPOS.Position
local moveEnd = SpotlightPOS.Position * CFrame.Angles(1, 0, 0)
local tweenInfo = TweenInfo.new(
0.5,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
-1,
true,
0
)
local tweenStart = TweenService:Create(SpotlightHead, tweenInfo, {moveStart = moveEnd})
local tweenStop = TweenService:Create(SpotlightHead, tweenInfo, {moveEnd = moveStart})
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.KeypadEight then return end
if UpdateCon then
UpdateCon:Disconnect()
UpdateCon = nil
end
UpdateCon = RunService.Heartbeat:Connect(function(dt)
tweenStart:Play()
SpotlightHead:SetPrimaryPartCFrame(tweenStart)
end)
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.KeypadEight then return end
if not UpdateCon then return end
tweenStop:Play()
UpdateCon:Disconnect()
UpdateCon = nil
end)
tween the spotlightPos instead of the SpotlightHead
you cant tween models because they have no CFrame
Note that you should be tweening only the primary part of the model and that the primary part should be the only anchored part in the model with the other parts welded to it.