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

How do I get a Part/Model to move to a specific point that is determined when I click?

Asked by 8 years ago
Edited 8 years ago

Ok so, I'm creating a Real Time Strategy game where I need units to be moved to a specific point. I've created a part called MoveCommand that when I click on the base plate it moves where ever the mouse.target is. So I want the Unit (that is going to be made out of bricks) to move to the MoveCommand location and it needs to update whenever the MoveCommand moves to a new location (I don't want it to move once and never move again). I don't want the Unit to teleport so I'd like it to move like below if possible

So the code below makes the unit move how I'd like it to and sets the speed ect..

for i = 1 , 500 do
workspace.Grunt.Head.CFrame = workspace.Grunt.Head.CFrame + Vector3.new(0,0,.03)
wait (.03)
end

(Head is a normal brick)

I've tried changing the Vector3 coordinates to a specific part position like this...

for i = 1,500 do
workspace.Grunt.Head.CFrame = workspace.Grunt.Head.CFrame + Vector3.new(workspace.MoveCommand.Position)
wait (.03)
end

The part wont budge its unanchored and I tried adding a humanoid to it so I could get it to move but it still didn't.

Any help would be Appreciated Thank You!

2 answers

Log in to vote
0
Answered by 8 years ago

Pretty easy, First get the players and then the mouse:

local player = game.Players.LocalPlayer --Get the player
local mouse = player:GetMouse() --Get the mouse

mouse.Button1Down:connect(function() --Funtion
    for i,v in pairs (game.Workspace.Units:GetChildren()) do --Get the brick you trying to move
        if v:IsA ("Model") then --Use the IsA method
            v.Humanoid:MoveTo((mouse.Hit).p)--Brick needs to have a humanoid inside
        end
    end
end)

If this helped remenber to accept my answer!

Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

If you are moving a model use MoveTo(). Note that MoveTo() will move all the parts in a model relative to the PrimaryPart(Meaning you want to figure out what position of the primary part will work for the entire model). If you don't know what the primary part is or haven't set it, its a property under model. If you are just trying to move a part, it should error in the output if the move was unsuccessful. Unanchoring the part will not affect moving the CFrame(an anchored part will move just fine). I could not find any errors in your code above and I ask that you edit it and add code blocks so its easier to read.

game.ModelName:MoveTo(Vector 3 Position) --moves all the parts in a model relative to the Primary Part position given in Vector3
0
I set the Primary part and messed with the code a little but now It just teleports to the position I want it to move to...I want it to slowly move there not teleport here is the code that is making it Teleport.... for i = 1,500 do workspace.Model1:MoveTo(workspace.MoveCommand.Position) wait (1) end DesertEagle234 55 — 8y
0
I would probably be more efficient to use bodyposition or something like that. Never done that to models though. dragonkeeper467 453 — 8y

Answer this question