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
1script.Parent.Touched:connect(function()
2    if game.StarterGui.Shop.Background.Visible == false
3        then
4        game.StarterGui.Shop.Background.Visible = true
5    end
6end)
7 
8--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

01Background = game.StarterGui.Shop.Background
02 
03function onClicked(mouse)
04    if Background.visible  == false
05        then
06        Background.Visible = true
07    end
08end
09 
10script.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
01script.Parent.ClickDetector.MouseClick:connect(function(plr)
02    local player = game.Players:WaitForChild(plr.Name)
03    local gui = player.PlayerGui
04 
05    if gui.Shop.Background.Visible == false then
06        gui.Shop.Background.Visible = true
07    else
08        gui.Shop.Background.Visible = false
09    end
10end)
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

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

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