I cannot quite figure this out. I know the script is wrong, but do not know what I have been doing. Basically, each time a person presses W, the x axis increases. But, I cannot quite figure out what I've been doing wrong. It only works once, and on rare occasion leads to errors. Can someone help figure out what I was doing wrong, or at least give me some tips? Thanks!
tool = script.Parent char = game.Workspace.char tool.Equipped:connect(function(mouse) mouse.KeyDown:connect(function(key) if key == "w" then char.Position = Vector3.new(-10, 3.2, -17.6) + Vector3.new(-5,3.2,17.6) end end) end)
Assuming that your trying to make the part keep moving 5 more the -x direction then this code should work :
tool = script.Parent x = -5 char = game.Workspace.char tool.Equipped:connect(function(mouse) mouse.KeyDown:connect(function(key) if key == "w" then x = x + -5 char.Position = Vector3.new(x, 3.2, -17.6) end end) end)
Also if you are trying to move a characters torso you should replace : char.Position = Vector3.new(x, 3.2, -17.6) with char.CFrame =CFrame.new(x, 3.2, -17.6)
It only works once because you're adding the position increase to the part's original position, so when you try to do it again it still gives you the same value. What you need to do is add the position increase to the part's current position, like so:
tool = script.Parent char = game.Workspace.char tool.Equipped:connect(function(mouse) mouse.KeyDown:connect(function(key) if key == "w" then char.Position = char.Position + Vector3.new(-5, 0, 0) end end) end)