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

[UPDATE] This Click Detector Script won't work?

Asked by 8 years ago
function open()
    game.StarterGui.ToolBar.Inventory.NIGHTVISION.nv.Value = true
end
script.Parent.MouseClick:connect(open)

Above, I fixed the script so It works now.Now I'm having trouble with the main script,

if script.Parent.Value == true then
    script.Parent.Parent.Nightvision.Visible = true
end

The script.Parent.Value is nv, the BoolValue which turns true when you click the click detector. For some reason, the ImageButton Nightvision isn't Visible, but won't turn Visible when the value is true. Any help?

Click the link below.

Click this link (relevant to the question I'm asking)

2 answers

Log in to vote
0
Answered by
davness 376 Moderation Voter
8 years ago

Your main script will never work because it is run only once, so you need to make it run multiple times:

while true do
    if script.Parent.Value == true then
     script.Parent.Parent.Nightvision.Visible = true
    end
    wait()
end

But this will "eat" too much resources, possibly making your script laggy. So, we will check the value everytime the value changes:

function check()
if script.Parent.Value == true then
     script.Parent.Parent.Nightvision.Visible = true
    end
end

game.Workspace.NightVisionGoggles.Part.ClickDetector.MouseClick:connect(check)

Protip 1: You can reduce your script lines if you connect your event directly to the function, like this:

game.Workspace.NightVisionGoggles.Part.ClickDetector.MouseClick:connect(function()
    if script.Parent.Value == true then
     script.Parent.Parent.Nightvision.Visible = true
    end
end)

Protip 2: When comparing booleans, you don't need the == or ~= operator, like this:

game.Workspace.NightVisionGoggles.Part.ClickDetector.MouseClick:connect(function()
    if script.Parent.Value then
     script.Parent.Parent.Nightvision.Visible = true
    end
end)

And let's return to the first script. Something looks obviously wrong:

function open()
    game.StarterGui.ToolBar.Inventory.NIGHTVISION.nv.Value = true -- here
end
script.Parent.MouseClick:connect(open)

You are updating everything in the StarterGui, so you will just get the updates once you respawn. To prevent this, you need to make the changes in PlayerGui. Fourtanely, the MouseClick event returns the player who clicked, making the things easier:

script.Parent.MouseClick:connect(function(p)
    game.Players:FindFirstChild(p.Name).PlayerGui.ToolBar.Inventory.NIGHTVISION.nv.Value = true
end)

Suddenly, I think we don't need two scripts. We can compile everything all-in-one, at your main script:



script.Parent.MouseClick:connect(function(p) local inv = game.Players:FindFirstChild(p.Name).PlayerGui.ToolBar.Inventory if not inv.NIGHTVISION.nv.Value then inv.NIGHTVISION.nv.Value = true inv.Nightvision.Visible = true end end)
0
Thanks so much! PyccknnXakep 1225 — 8y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

Your problem is, in the function, you're accessing the StarterGui instead of the PlayerGui. The PlayerGui is an objects that contains all the GUIs available to a player. If you didn't know, there is a parameter in the MouseClick event of a ClickDetector. The parameter is the Player who clicked it. So, here is your fixed up script:

script.Parent.MouseClick:connect(function(player)
    player.PlayerGui.ToolBar.Inventory.NIGHTVISION.nv.Value = true
end)

With the fix applied above, the value will be true, so your 2nd script will be able to work. Sadly, it won't just yet! I noticed that you don't have a loop, or a changed event in your script, to keep toggling NIGHTVISION! You could do this by 2 ways: A Changed event, or a while loop.

Changed Event:

script.Parent.Changed:connect(function()
    if script.Parent.Value == true then
        script.Parent.Parent.Nightvision.Visible = true
    end
end)

Loop:

while wait() do
    if script.Parent.Value == true then
        script.Parent.Parent.Nightvision.Visible = true
    end
end
0
I wish I could accept both answers, but yours is awesome too! Thanks! PyccknnXakep 1225 — 8y
0
No problem Shawnyg 4330 — 8y

Answer this question