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

Why is my "if" statement running when the conditions are not met?

Asked by
Faazo 84
6 years ago
Edited 6 years ago

I want to make it so that when I walk into the detector it opens a shop but I want to make it so when a child is added to PlayerGui it changes the variable so it doesn't make more shops. Here is my script:

    local Players = game:GetService("Players")
    local LocalPlayer = Players.LocalPlayer
    local PlayerGui = LocalPlayer.PlayerGui
    local Detector = game.Workspace.ShopDetector
    local RS = game:GetService("ReplicatedStorage")

local ShopOpen = ("False")

Detector.Touched:Connect(function()    
    if ShopOpen == ("False") then 
    RS.Shop:Clone().Parent = game.Players.LocalPlayer.PlayerGui  
    end
end)

 PlayerGui.ChildAdded:Connect(function()
    local ShopOpen = ("True") 
    print (ShopOpen)    
end)

PlayerGui.ChildRemoved:Connect(function()
    local ShopOpen = ("False")    
end)

its a local script by the way. It is in startergui. For some reason it doesn't work. It just opens a LOT of shops. I have tried writing print (ShopOpen) when a child is added and it wrote true in the output but it still made more shops. Nothing even comes up in the output. I'm certain it has something to with the if statement. Help would be greatly appreciated. Thanks!

2 answers

Log in to vote
1
Answered by
lukeb50 631 Moderation Voter
6 years ago

Your script just has a lot of little mistakes that add up and make the script less than ideal.

1)Brackets around variables is in no way needed

2)Storing a boolean as a string is not a good idea. If you need true/false values, store them as such.

3)Don't detect when a child is added if the same script is making it. Just change the variable in the same function.

4)Don't use local unless the variable is truly local to that specific function. By using local, you pretty much create 3 different variables each with their own different value.

Here is a revised script that should work

    Players = game:GetService("Players")
    LocalPlayer = Players.LocalPlayer
    PlayerGui = LocalPlayer.PlayerGui
    Detector = game.Workspace.ShopDetector
    RS = game:GetService("ReplicatedStorage")

ShopOpen = false

Detector.Touched:Connect(function()    
    if ShopOpen == false then 
    RS.Shop:Clone().Parent = game.Players.LocalPlayer.PlayerGui  
    ShopOpen=true
    end
end)

PlayerGui.ChildRemoved:Connect(function()
    ShopOpen = false    
end)
0
Thank you so much. I'm new to scripting so sorry it's really bad. I was taught to use local when writing a variable so thats why I did it. Anyways I can't thank you enough! C: Faazo 84 — 6y
0
I'm happy I could help. Could you accept the answer? It helps us both if you do. lukeb50 631 — 6y
0
How do I accept it? Faazo 84 — 6y
0
There is a button below these comments called "Accept Answer" lukeb50 631 — 6y
View all comments (2 more)
0
There is only a button that says report. Faazo 84 — 6y
0
Not sure whats causing that, it may appear later. lukeb50 631 — 6y
Ad
Log in to vote
-1
Answered by
Plieax 66
6 years ago

What you need to do here is don't make it a clone do this.

if ShopOpen == false then
LocalPlayer.PlayerGui:WaitForChild ("RS.Shop").Enabled = true
0
If you have questions or it doesn't work tell me Plieax 66 — 6y
0
Cloning, while maybe not ideal, is fine. You missed the bigger errors. lukeb50 631 — 6y

Answer this question