Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

When W is pressed, Vector3 of Part changes?

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)

1 comment

0

do char.CFrame = CFrame.new(Vector3.new(-10, 3.2, -17.6) + Vector3.new(-5,3.2,17.6)) that should fix it.

2 answers

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.