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!
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)
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