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

How to set Model's position with Vector3 coordinates ?

Asked by 5 years ago
Edited 5 years ago

I've tried moving a model to a specific position. However, the position is in Vector3. I used SetPrimaryPartCFrame but i dont know how to convert Vector3 in CFrame

Here is my script :

game.ReplicatedStorage.RemoteEvent.OnServerEvent:connect(function(player, mousePosition)
local model = game.Workspace.Model:Clone()
        model.Parent = game.Workspace
        model.PrimaryPart = spike.Part
        print(mousePosition)
        model:SetPrimaryPartCFrame(mousePosition) -- This doesnt work : it puts the model to the spawn position 0,0,0
end)

This part is from the LocalScript that fires the event :

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local mousePosition = mouse.Hit.p

game.ReplicatedStorage.RemoteEvent:FireServer(mousePosition)

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Models have a method for this as well, and it is called MoveTo.

:MoveTo() acts similarly to :SetPrimaryPartCFrame() in that it will move your part to the desired CFrame/Position albeit that Vector3 is much different from CFrame.

So, in the code you have provided -- you would do:

-- Server Script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:connect(function(player, mousePosition)
    local model = game.Workspace.Model:Clone()
    model.Parent = game.Workspace
    model:MoveTo(mousePosition)
end)

This will set the model's children to the correct locations relative to mousePosition.

Other solutions:

  • Send Mouse.Hit instead of Mouse.Hit.p, this is the CFrame location of the same point.

  • Convert Mouse.Hit.p into a CFrame by using CFrame.new(Mouse.Hit.p)

Ad

Answer this question