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

How do you use UIS for mouse functions?

Asked by 5 years ago

I'm trying to make it so where the player clicks relative to the game, it creates a new part at the position of their mouse.

It's incredibly easy to make this with :GetMouse() but on the wiki it recommends to use UserInputService instead for mouse stuff, but I don't know how to get it to work, it creates the parts way above where I clicked it and it isn't positioning to where I click. Why isn't this working?

local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:Connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local p = Instance.new('Part')
        p.Parent = workspace
        p.Position = input.Position
        p.Anchored = true
    end
end)

UserInputService.InputChanged:Connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        print('hi')
    end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

Hi Yaba,

I think the wiki might be saying it's better to use UserInputService as the event and not as the Position of the part. So, use the mouse to get the position for the part, but use UserInputService as the event that fires. I don't know why the wiki would suggest this, I've never heard of this suggestion but, I'm assuming it does.

This is how it would look like:

local uis = game:GetService("UserInputService");
local players = game:GetService("Players");
local player = players.LocalPlayer;

local mouse = player:GetMouse();

uis.InputBegan:Connect(function(input, gp)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local p = Instance.new('Part')
            p.Parent = workspace
            p.Position = mouse.hit.p;
            p.Anchored = true

    end
end)

Hope i helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

Ad

Answer this question