#Script that creates a new part when the player right clicks doesn't work

1 messages · Page 1 of 1 (latest)

fleet ice
#

My script is a local script placed inside of a rig and what I need it to do is when a player right clicks, it spawns a part where the click happened.

The code is very flawed (although no errors occur) and I need some guidance on how to do this

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

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.MouseRightButton then
local goal = Instance.new("Part")
goal.Position = mouse.x and mouse.y
goal.Anchored = true
goal.CanCollide = false

    print("Right Click")
end

end)

#

also how would I name my part when I spawn it in

north shuttle
#
  1. input.KeyCode, Mouse Clicks are not detected by this. you need to use input.UserInputType instead!

  2. How you position the part: Your using 2D values, x, y instead you need to get the Hit.p of the mouse, rather than the values of the mouse if you want it to be like that.

  3. it's not Parented anywhere.

#

Also btw, your using Right Click not Left Click, i don't know if thats intentional 🤷‍♂️

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

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    
    if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Use MouseButton2 if it's supposed to be at the right mouse button lol
        local goal = Instance.new("Part")
        goal.Parent = workspace
        goal.Position = mouse.Hit.p
        goal.Anchored = true
        goal.CanCollide = false

        print("Left Click ", goal.Position)
    end
end)
#

Hope this helps!

#

also as for the name.. Uhh you can name it whatever

#

🤷‍♂️

fleet ice
#

Thank you!

primal egretBOT
#

studio** You are now Level 3! **studio

fleet ice
#

I will try out this code!

north shuttle
#

if your goal is to just name parts like this individually then you can simply use a Count variable

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

local Count = 0


UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    
    if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Use MouseButton2 if it's supposed to be at the right mouse button lol
        local goal = Instance.new("Part")
        goal.Name = "Part ".. tostring(Count)
        goal.Parent = workspace
        goal.Position = mouse.Hit.p
        goal.Anchored = true
        goal.CanCollide = false
        
        Count += 1

        print("Left Click ", goal.Position)
    end
end)
``` like this
fleet ice
#

also where would this be located?

north shuttle
#

StarterGui is what i put this on

#

Goodluck! ^^

fleet ice
#

Thanks!

#

It works well