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

How to set the position of a Explosion using Mouse.Hit?

Asked by 8 years ago

Mouse.Hit is a CFrame. The explosion Instance takes in a Vector3 property. There's no error in my script. The whole script does execute. Here's the script,

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

mouse.Button1Down:connect(function()
    local Boom = Instance.new("Explosion", game.Workspace)
    Boom.Position = Vector3.new(mouse.Hit)
end)

When I click somewhere, the explosion does not appear. Also, when I checked workspace, the explosion was in workspace, but the position property was 0,0,0.

What's the problem?

Test

Thank you.

2 answers

Log in to vote
1
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago

According to the wiki, you cannot construct a Vector3 from a CFrame. The only possible constructors are Vector3.new(number x, number y, number z), Vector3.FromNormalId(Enum.NormalID normal) and Vector3.FromAxis(Enum.Axis axis).

You could however retrieve the p property of your mouse.Hit CFrame, which is a Vector3.

-- As simple as that.
explosion.Position = mouse.Hit.p

Hope that helps.

Ad
Log in to vote
0
Answered by 8 years ago

So basically like you said, calling mouse.hitwill return a CFrame so that means we have to make it a Vector3. There are probably better ways to do this but a simple way call the X,Y and Z coordinates from the CFrame which makes it a Vector3 as seen below.

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

mouse.Button1Down:connect(function()

    local Boom = Instance.new("Explosion", game.Workspace)
    Boom.Position = Vector3.new(mouse.Hit.x, mouse.Hit.y, mouse.Hit.z) -- Converting CFrame into Vector3

end)

Hope this helped, Have fun scripting!

Answer this question