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

What's wrong with this level 1 easy script?!

Asked by 9 years ago
script.Parent.Touched:connect(function()
    if game.StarterGui.Shop.Background.Visible == false
        then
        game.StarterGui.Shop.Background.Visible = true
    end
end)

--It's pretty simple, when someone clicks the brick, the background of a shopgui becomes visible. what's broken?

3 answers

Log in to vote
0
Answered by 9 years ago

You said when they click the brick. In this situation you need a Clickdetector! Insert a click detector in the part. The code should be like this

The Function Is A OnClicked, Not An OnTouched

It Is Also Easier to Define and Could Make It More efficent

Background = game.StarterGui.Shop.Background

function onClicked(mouse)
    if Background.visible  == false
        then
        Background.Visible = true
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I Hope This Helped You!

0
Thanks, but it still didn't help. Could it be there's another script interfering with this one? james24dj 90 — 9y
0
Is there anything popping up in the output? TayLife12 69 — 9y
0
function onClicked(mouse) if game.StarterGui.Shop.Backg:8: attempt to index global 'script' (a nil value) james24dj 90 — 9y
0
Another Script could Be Interfering Let Me Update My Answer TayLife12 69 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
script.Parent.ClickDetector.MouseClick:connect(function(plr)
    local player = game.Players:WaitForChild(plr.Name)
    local gui = player.PlayerGui

    if gui.Shop.Background.Visible == false then
        gui.Shop.Background.Visible = true
    else
        gui.Shop.Background.Visible = false
    end
end)
Log in to vote
0
Answered by 9 years ago

This problem is quite simple! You are accessing the StarterGui which is not what you want for live events. Instead you need the PlayerGui. This can be accomplished using the :GetPlayerFromCharacter() method

script.Parent.Touched:connect(function(hit)--Hit is what touches the part.
    if game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.Shop.Background.Visible = false--we get the Player from game.Players
        then
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.Shop.Background.Visible = true
    end
end)

That should be it! Hope this helped!!!

0
I'll test it. Thanks to all of you! But, why isn't it StarterGui when it's IN the startergui? james24dj 90 — 9y
0
StarterGui is what puts the guis into all the player's perosonal guis minikitkat 687 — 9y

Answer this question