Basically, I am trying to detect if a player has any of the folders in the ApartmentTags table. If they don’t, the script takes away 700 candy away from the player and executes a RemoteEvent. If they do, the script doesn’t allow them to interact with the purchase button.
The detecting part of the script works, as it prints out if the player has a folder from ApartmentTags.
However, when the player doesn’t have any of these folders, the game doesn’t execute this part of the script:
local localPlayer = game.Players.LocalPlayer
local PlayerGui = localPlayer.PlayerGui
local PurchaseButton = PlayerGui.PropertyPurchase.Menu.ScrollingFrame.Apartment22.PurchaseButton
local AP22RemoteEvent = game.ReplicatedStorage.ApartmentPurchase.Apartment22
PurchaseButton.Activated:Connect(function()
local ApartmentTags = {
Apartment24Tag = localPlayer:FindFirstChild("Apartment24"),
Apartment26Tag = localPlayer:FindFirstChild("Apartment26"),
Apartment28Tag = localPlayer:FindFirstChild("Apartment28"),
Apartment30Tag = localPlayer:FindFirstChild("Apartment30"),
}
for i, tag in pairs(ApartmentTags) do
if tag then
print(tag)
else if not tag then
localPlayer:FindFirstChild("CandyStats")
local playerCandy = localPlayer.CandyStats:FindFirstChild("Candy")
playerCandy.Value = playerCandy.Value - 700
PurchaseButton.Visible = false
AP22RemoteEvent:FireServer()
end
end
end
end)
Disregarding any of the other potential issues with your code, this should tentatively fix your existing problem you’ve asked about.
Whenever you have a condition inside of a for loop, it is always important to remember that it’ll check for every element of the table unless manually “stopped” if a certain condition matches.
For what you’re attempting to do, you can break the loop and the rest of the function’s execution by adding a return inside of your for loop if at least one of the folders in your ApartmentTags table is found. If none of the folders are found, only then the code will run the consequential execution of taking out 700 candies once in that specific iteration.
Give the below code a shot and see if it yields the intended behavior.
local localPlayer = game.Players.LocalPlayer
local PlayerGui = localPlayer.PlayerGui
local PurchaseButton = PlayerGui.PropertyPurchase.Menu.ScrollingFrame.Apartment22.PurchaseButton
local AP22RemoteEvent = game.ReplicatedStorage.ApartmentPurchase.Apartment22
PurchaseButton.Activated:Connect(function()
local ApartmentTags = {
Apartment24Tag = localPlayer:FindFirstChild("Apartment24"),
Apartment26Tag = localPlayer:FindFirstChild("Apartment26"),
Apartment28Tag = localPlayer:FindFirstChild("Apartment28"),
Apartment30Tag = localPlayer:FindFirstChild("Apartment30"),
}
for i, tag in pairs(ApartmentTags) do
if tag then
print(tag.." exists!")
return
end
end
print("None of the tags could be found.")
localPlayer:FindFirstChild("CandyStats") -- Not sure what this is for.
local playerCandy = localPlayer.CandyStats:FindFirstChild("Candy")
playerCandy.Value = playerCandy.Value - 700
PurchaseButton.Visible = false
AP22RemoteEvent:FireServer()
end)