Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

When W is pressed, Vector3 of Part changes?

Asked by
bloxxyz 274 Moderation Voter
10 years ago

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)
0
do char.CFrame = CFrame.new(Vector3.new(-10, 3.2, -17.6) + Vector3.new(-5,3.2,17.6)) that should fix it. DragonSkyye 517 — 10y

2 answers

Log in to vote
1
Answered by
samfun123 235 Moderation Voter
10 years ago

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)

Ad
Log in to vote
0
Answered by 10 years ago

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)

Answer this question