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

Unable to cast Vector3 to CoordinateFrame ?

Asked by 5 years ago
local tool = script.Parent
local player = game.Players.LocalPlayer
local house = game.Lighting.House
target = game.Players.LocalPlayer:GetMouse().Target

script.Parent.MouseButton1Click:Connect(function()
    wait(target)
    local part = house:Clone()
    part.Parent = workspace
   part:SetPrimaryPartCFrame(Vector3.new(target))
end)

I have problem that been more than 2 months with trying and i didn't know how to fix it, i want the house copy at mouse position, I click the button then i click again and the house copy itself at second click position but Setprimarypartcframe isn't working, please help i am starting to get annoying for it.

0
Answers not there(o). Use the "Answer this question) box. ArtyomAL3X 12 — 5y
0
use cframe instead of vector3 LoganboyInCO 150 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

First thing - you might want to name your variables something clearer - the part variable is a model right? Why not name it houseModel or something?

Anyway - the issue is that part:SetPrimaryPartCFrame() is expecting a CFrame, but you are passing a Vector3. This is a simple fix, just wrap your Vector3 in a CFrame contstructor:

local tool = script.Parent
local player = game.Players.LocalPlayer
local house = game.Lighting.House
target = game.Players.LocalPlayer:GetMouse().Target

script.Parent.MouseButton1Click:Connect(function()
    wait(target)
    local part = house:Clone()
    part.Parent = workspace
    part:SetPrimaryPartCFrame(CFrame.new(Vector3.new(target)))
end)

Hope this helps, happy coding :)

0
I fixed it by putting part:SetPrimaryPartCFrame(target) and gives the variable "local" but the problem is when the house is copied it goes to sky because my cursor is on sky which is i want it to go on the ground how do i do that ? xxibadrxx 0 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

There are 2 problems causing this error: You are creating a new Vector3 using an object as a parameter You are setting the CFrame of a part to a Vector3

The first problem is very easy to fix: Simply access the .Position property of target, and forget the entire Vector3.new() part, this'll give you the Vector3 you want.

The second problem is actually also quite easy to fix: If you want to set the CFrame of something to a new CFrame, you can do so by using CFrame.new(...) This'll accept 1-2 parameters, the first is the position, the second is what the CFrame points towards. In this case, use target.Position as the first parameter, and use that for :SetPrimaryPartCFrame()

Implementing these 2 changes, your code will look like this:

local tool = script.Parent
local player = game.Players.LocalPlayer
local house = game.Lighting.House

target = game.Players.LocalPlayer:GetMouse().Target

script.Parent.MouseButton1Click:Connect(function()
    wait(target)
    local part = house:Clone()
    part.Parent = workspace
    part:SetPrimaryPartCFrame(CFrame.new(target.Position))
end)
0
I fixed it by putting part:SetPrimaryPartCFrame(target) and gives the variable "local" but the problem is when the house is copied it goes to sky because my cursor is on sky which is i want it to go on the ground how do i do that ? xxibadrxx 0 — 5y
0
Multiply target.Position with Vector3.new(1,0,1) and then add Vector3.new(0,Y,0). and replace Y with your ground level. That's about how far I can help you with what I know about the situation right now. DarkageMast3r 140 — 5y

Answer this question