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

how to make a gui appear when player clicks on HitBox? (Without an actual click detector)

Asked by 6 years ago

So i already made a script to make a selection box hover over an item and when the mouse doesn't hover over the item, the selection box gets destroyed....

now my goal is to, when the mouse.Pointer hovers over the item and when the player clicks the right clicks the item, a Gui should appear directly above it.. Here is what I have so far..

while wait() do -- for ever loop
local a = game:GetService("Players").LocalPlayer:GetMouse() -- players mouse
if a.Target == "Hitbox" then -- if the players mouse hovers over hitbox then
    local b = script.Parent.Parent.ChangeItem -- a variable is created in place of a gui...
    a.MouseButton2Down:connect(function() -- when the player clicks that spot, "HitBox" then
        print("O_o") -- it should print that face
        b.Enabled  =  true -- it should make the gui enabled
        b.Frame.Position = UDim2.new(0,a.X,0,a.Y) -- the frame of the gui should be directly on the mouse
    end)
end
end

Script ^ in StarterGui

thanks for any help, variables are in the code, tell what they are...

2 answers

Log in to vote
0
Answered by
Bellyrium 310 Moderation Voter
6 years ago

Can I help with this code in general?

a while loop is probably the worst way to go. Not only can it make a small delay, it causes lag. I would recommend you use an event. For this I would probably recommend RunService.RenderStepped:Connect() This makes it so the script never actually pauses because it will still technically be processing. There's also some small issues like a.Target == "Hitbox" might be an issue as mouse.Target returns an Object not a String. Take a look at some of the changes I made and it should help you!

local a = game:GetService("Players").LocalPlayer:GetMouse() -- you should only need to name this once
a.MouseButton2Down:connect(function() -- easiest if the function just checks for the target instead of nesting. To create an event you would need to use the thread scheduler I believe. At-least that would be the efficient way.
    if a.Target == "Hitbox" then -- this line confuses me
        local b = script.Parent.Parent.ChangeItem 
        print("O_o") 
            b.Enabled  =  true
            b.Frame.Position = UDim2.new(0,a.X,0,a.Y) 
    end
end)

game:GetService("RunService").RenderStepper:Connect(function() -- this should take place of the while loop, check it out.
    if a.Target and a.Target.Name == "HitBox" then -- i changed this line to something that makes more sense, although its possible you were correct to begin with.
        print">:O"
    end
end)

0
hmm i will try this bellyrium.. looks quite right to me greatneil80 2647 — 6y
Ad
Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
6 years ago

The players' mouse doesn't have a MouseButton2Click function. Instead, it's just named Button2Down. Same goes for button1.

So, your script should look something like this instead. Also, isntead of using a while loop, easily use the function Mouse:Move().

local a = game:GetService("Players").LocalPlayer:GetMouse() -- players mouse
a.Move:connect(function()
if a.Target == "Hitbox" then -- if the players mouse hovers over hitbox then
    local b = script.Parent.Parent:WaitForChild("ChangeItem") -- a variable is created in place of a gui...
    a.Button2Down:connect(function() -- when the player clicks that spot, "HitBox" then
        print("O_o") -- it should print that face
        b.Enabled  =  true -- it should make the gui enabled
        b.Frame.Position = UDim2.new(0,a.X,0,a.Y) -- the frame of the gui should be directly on the mouse
    end)
end
end
end)

If I helped and this works, make sure to accept my answer!

print("Blox on!")
0
aha but you see move is a script it has nothing to do with the mouse.... greatneil80 2647 — 6y
0
Actually... It does ... Wiscript 622 — 6y
0
All of the mouse events: https://gyazo.com/09adf62214681200bda82d4bfc57983c Wiscript 622 — 6y

Answer this question