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

Clickdetector doesnt detect a click?

Asked by
Jac_00b 157
3 years ago

So, I'm trying to make a basic GUI script. When you click on the tool, a gui will pop up and if you click "yes" it will take away $5 of your money. If you click "no" the gui will close. The problem is: It's not that part of the script thats not working, its actually the clickdetector for the tool. The print statement after it doesn't run. Here is the script:

local player = game.Players.LocalPlayer
local mainGUI = player.PlayerGui.BuyScreen
local yes = mainGUI.Yes
local no = mainGUI.No
local info = mainGUI.Info
local dollars = player.leaderstats.dollars
local tool = script.Parent.Parent
local price = 5
script.Parent.ClickDetector.MouseClick:Connect(function()
    print("Click!")
    mainGUI.Enabled = true
    info.Text = "Are you sure you want to buy "..tool.Name.." for $"..price.."?"
    yes.MouseButton1Click:Connect(function()
        if dollars.Value >= price then
            dollars.Value = dollars.Value - price
            local clonetool = tool:Clone()
            clonetool.Parent = player.Backpack

            local clonetool = tool:Clone()
            clonetool.Parent = player.StarterGear

            mainGUI.Enabled = false

            no.MouseButton1Click:Connect(function()
                mainGUI.Enabled = false
            end)
        end
    end)
end)

Anyway, I would really appreciate it if you could help.

2 answers

Log in to vote
0
Answered by
Pupppy44 671 Moderation Voter
3 years ago

You cant have MouseClick in a localscript, that's why it isn't working. You must have it in a normal script.

To get the player that clicked, do this

script.Parent.ClickDetector.MouseClick:Connect(function(Player) -- Player is the player who clicked it
0
but why does the wiki says it should work? https://developer.roblox.com/en-us/api-reference/class/ClickDetector Jac_00b 157 — 3y
0
Not sure, I tested it myself and local scripts didn't work with a Clickdetector Pupppy44 671 — 3y
0
Same, localscripts didn't work when I tested them out. It seems my answer isn't what you're looking for nekosiwifi 398 — 3y
0
umm, it still didn't work. In fact, I put a print at the beginning of the script and IT DOESNT WORK, which means my script doesn't even run? Jac_00b 157 — 3y
View all comments (6 more)
0
I even restarted studio with no luck. Im starting to think this may be a bug. Even completely removing the clickdetector function, it still doesn't show up. Prints dont work either. Jac_00b 157 — 3y
0
this is 100% a bug. I literally deleted all the code from the script and did print("test") and it didnt come up Jac_00b 157 — 3y
0
ClickDetectors do work with local scripts. It's the fact that you guys have your local scripts inside the workspace. Local scripts don't work inside workspace. It's not a bug. You can see where local scripts work here: https://developer.roblox.com/en-us/api-reference/class/LocalScript xInfinityBear 1777 — 3y
0
The localscript is inside of a tool's handle, which IS allowed by the server. Jac_00b 157 — 3y
0
wait... THE TOOL IS INSIDE OF THE WORKSPACE... im so sorry Jac_00b 157 — 3y
0
yup, it works now Jac_00b 157 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

It appears you are attempting to use ClickDetectors for Gui Buttons. ClickDetectors are for detecting mouse clicks on 3d objects whilst a mouse button click detects clicks on 2d Ui objects.

Here's your script but modified:

local player = game.Players.LocalPlayer
local mainGUI = player.PlayerGui.BuyScreen
local yes = mainGUI.Yes
local no = mainGUI.No
local info = mainGUI.Info
local dollars = player.leaderstats.dollars
local tool = script.Parent.Parent
local price = 5
function onClick()
    print("Click!")
    mainGUI.Enabled = true
    info.Text = "Are you sure you want to buy "..tool.Name.." for $"..price.."?"
    yes.MouseButton1Click:Connect(function()
         if dollars.Value >= price then
                dollars.Value = dollars.Value - price
                local clonetool = tool:Clone()
                clonetool.Parent = player.Backpack

                local clonetool = tool:Clone()
                clonetool.Parent = player.StarterGear

                mainGUI.Enabled = false
        end --Forgot to close off these function and if statements?
    end)
    no.MouseButton1Click:Connect(function()
        mainGUI.Enabled = false
        end)
  end
end)
script.Parent.MouseButton1Click:Connect(onClick) --MouseButton1Clicks detects left clicks on a 2d UI Object

I'm sorry if this was not what you were looking for.

Answer this question