I have a script that should make a model move based on keybinds.
When I press a key, the model moves in a certain direction, in this case, it is rotating in one angle.
When I release it, it stops moving entirely.
What I want from this script is to make a spotlight model rotate smoothly with the use of tweening, when I press and release a key, by gradually speeding/increasing up to a number, let’s say 1, then upon release, slow/lower down back to 0.
Yet, this script which I have been trying to tweak and ask for help to fix, hasn’t been successful in getting it to work, which is why I’m coming here again to try and get this script fixed.
Currently, there is an error shown with the current code it has gotten to, which I will show below.
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
local SpotlightCurrentPos = SpotlightPos.Position
local moveStart = SpotlightCurrentPos
local moveEnd = SpotlightCurrentPos * CFrame.Angles(1, 0, 0)
local tweenInfo = TweenInfo.new(
0.5,
Enum.EasingStyle.Quad,
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(moveStart)
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)
Here is a video of the original movement of the spotlight:
If anyone is able to clean this code up or provide solutions as to what I can do to get this script working, please let me know.
Any help is greatly appreciated.
(While not entirely necessary, it would also be easy to work this out on easier methods of communication, if the topic gets filled up too much)