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

Not moving when it's clicked again, why?

Asked by 8 years ago
Edited 8 years ago

Was testing something I learned from youtube and the script looked like.

local p = Instance.new("Part",workspace)
p.Anchored = true
p.Position = Vector3.new(0,0,0)
p.Size = Vector3.new(4,1,2)

local cd = Instance.new("ClickDetector", p)

function move()
      p.Position = Vector3.new(5,0,0)     
end

p.ClickDetector.MouseClick:connect(move)

It's supposed to move when I click it, it moves, but it only moves once and when clicked again nothing happens.

So I was wondering if any of you could help.

1 answer

Log in to vote
0
Answered by 8 years ago

The reason it "doesn't move" is because the coordinate that the part should move to is unchanged, meaning that the script is running your code, but since you're repeatedly moving the part to the same coordinate, Vector3.new(5, 0, 0), it looks like nothing is changing, and you can test this by using print statements. If you'd like to see a change on every click, try something along these lines:

local p = Instance.new("Part",workspace)
p.Anchored = true
p.Position = Vector3.new(0,0,0)
p.Size = Vector3.new(4,1,2)

local cd = Instance.new("ClickDetector", p)

function move()
      p.Position = p.Position + Vector3.new(5,0,0)     
end

p.ClickDetector.MouseClick:connect(move)

The script doesn't undergo any major changes, but in this script, it'll move the part 5 studs on the x axis every time you click it, because it is adding 5 studs to the x axis on every click, rather than setting the position of the part to a predetermined coordinate.

If this helped you, please remember to accept the answer :)

1
thanks a bunch ;) judegirl99 19 — 8y
0
No problem :D DepressionSensei 315 — 8y
Ad

Answer this question