Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

6 comments

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:

0

I'm happy I could help. Could you accept the answer? It helps us both if you do.

0

How do I accept it?

0

There is a button below these comments called "Accept Answer"

0

There is only a button that says report.

0

Not sure whats causing that, it may appear later.

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

2 comments

0

If you have questions or it doesn't work tell me

0

Cloning, while maybe not ideal, is fine. You missed the bigger errors.

This discussion is preserved as it was. New answers, comments, and votes are closed.