Hi all,
I am creating a custom movement system. Altho I am able to unbind the A and D keys from the default movement, the tween I created to change the position doesn’t work. Here’s the localscript under starterplayerscripts:
local UserInputService = game:GetService("UserInputService")
ContextActionService:UnbindAction("moveRightAction")
ContextActionService:UnbindAction("moveLeftAction")
local TweenService = game:GetService("TweenService")
game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local goal1 = {}
goal1.Position = Vector3.new(humanoidRootPart.Position.X, humanoidRootPart.Position.Y, humanoidRootPart.Position.Z + 1)
local tweenInfo1 = TweenInfo.new(5)
local tween1 = TweenService:Create(humanoidRootPart, tweenInfo1, goal1)
local goal2 = {}
goal2.Position = Vector3.new(humanoidRootPart.Position.X, humanoidRootPart.Position.Y, humanoidRootPart.Position.Z - 1)
local tweenInfo2 = TweenInfo.new(5)
local tween2 = TweenService:Create(humanoidRootPart, tweenInfo2, goal2)
local isAPressed = UserInputService:IsKeyDown(Enum.KeyCode.A)
local isDPressed = UserInputService:IsKeyDown(Enum.KeyCode.D)
UserInputService.InputBegan:Connect(function()
if isAPressed then
tween1:Play()
end
if isDPressed then
tween2:Play()
end
end)
Thanks. I may or may not be able to reply instantly now.
When you run a function and set it to a variable the returned value of the function gets stored into the variable not the function itself If you want to store a function into a variable the only way ifs probably tables and metamethods.
So when you print(isAPressed) you will always get a false.
Instead you can simply used the inputObject that is passed in automatically.
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.KeyCode == Enum.KeyCode.A then
tween1:Play()
elseif InputObject.KeyCode == Enum.KeyCode.D then
tween2:Play()
end
end)
to unbind the default A and D keys, you can use this: (must be a local script)
local Controls = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"):WaitForChild("ControlModule"))
Controls:Disable()
Well, according to the wiki page of ‘IsKeyDown’ it seems as if it only gets the current status (the current status is when the script is first ‘fired’) also using UserInputService to bind keys is highly unrecommended, instead you should use this (still a local script)
--no errors btw
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
tween1:Play()
elseif input.KeyCode == Enum.KeyCode.D then
tween2:Play()
end
end)
Hmm. That’s strange. Does debuging show that it runs?
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
print("A is pressed")
tween1:Play()
elseif input.KeyCode == Enum.KeyCode.D then
print("D is pressed")
tween2:Play()
end
end)
If it doesn’t then the problem might be that your code might be stuck or is looping further up. If it does run then that means the tween is wrong somehow.
Uhh okay so while doing some midnight development yesterday I got the tween to work, it also prints now, but its kinda sus that as soon as I turn I lose all control over the character, everything breaks and resize and its just so weird.
I am confused why your script has a tween time of 5 seconds but anyway, I don’t recommend using tweens for this, I would just detect if they pressed and runs a loop depending on the key pressed.
Script, tween free
local player = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local ContextActionService = game:GetService("ContextActionService")
ContextActionService:UnbindAction("moveRightAction")
ContextActionService:UnbindAction("moveLeftAction")
local char = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
local isAPressed = UserInputService:IsKeyDown(Enum.KeyCode.A)
local isDPressed = UserInputService:IsKeyDown(Enum.KeyCode.D)
UserInputService.InputBegan:Connect(function()
if isAPressed then
while isAPressed do -- RUNS IF YOU'RE HOLDING IT
humanoidRootPart.Position -= Vector3.new(0,0,0.1)
wait() -- must add wait otherwise rbolox kil ur pc
end
end
if isAPressed then
while isAPressed do -- RUNS IF YOU'RE HOLDING IT
humanoidRootPart.Position += Vector3.new(0,0,0.1)
wait() -- must add wait otherwise rbolox kil u pc
end
end
end)