简体   繁体   中英

How to spawn a part at the current mouse position in Roblox Lua?

I've been working on a script for Roblox. Here's the code:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Activation = 
Instance.new("Sound",game.Players.LocalPlayer.Character.Head)
local char = Player.Character
local hum = char.Humanoid
local root = char.HumanoidRootPart

UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
    Activation.SoundId = "rbxassetid://1581091676" --Plays Mangekyou Sharingan Activation Sound.
    Activation:Play()
    wait(0.3)       
    game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://76285632" --When F is pressed, face texture changes to sharingan decal.
    game:GetService("Chat"):Chat(Player.Character.Head, "Mangekyou Sharingan!")
end
end)

UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
    Activation.SoundId = "rbxassetid://1580990602" --Plays Amaterasu Activation Sound.
    Activation:Play()
    game:GetService("Chat"):Chat(Player.Character.Head, "Amaterasu!")
    local Target = Instance.new("Part") --makes a part
    Target.Parent = game.Workspace
    Target.Position = Vector3.new(Mouse.target.Position) --makes the part spawn where the mouse is 
    Target.Transparency = 1 
    Target.Anchored = true 
    Target.CanCollide = false 

    local Amaterasu = Instance.new("Fire")
    Amaterasu.Parent = game.Workspace.Part
    Amaterasu.Color = Color3.new(0,0,0)
    Amaterasu.SecondaryColor = Color3.new(0,0,0) --amaterasu properties
    Amaterasu.Size = 25

    local R = Instance.new("RocketPropulsion") --rocket propulsion, parents amaterasu
    R.Parent = Amaterasu
    R.MaxThrust = 300
    R.ThrustP = 30
    R:Fire()
end
end)

UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.G then
    game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://22557247" --When G is pressed, face texture changes back to normal.(leaves face blank isnt working :/)
end
end)
-----------------------------

What I need help with is spawning a the part that parents the amaterasu fire at the current position of my mouse in game. I have researched and tried both Target.Position = Vector3.new(Mouse.target.Position) , and Target.Position = Vector3.new(Mouse.Hit) .

These both don't work and the end result is the part spawning in the middle of the baseplate no matter where my mouse's position is in game.

Here is the ticket

local Part = Instance.new("Part",game.Workspace);
Part.CFrame = Mouse.Hit;

What we need is a CFrame . So we'll do:

Target.CFrame = CFrame.new(Mouse.target.Position)
Target.CFrame = CFrame.new(Mouse.target.Position)

That Should Work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM