First, tab your code correctly.
itembutton.MouseButton1Click:connect(function()
for i, v in pairs(game.ReplicatedStorage:GetChildren()) do
if v.Name == player.Name and v.Coins.Value >=100 and (not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) then
v.Coins.Value = v.Coins.Value - amount
game.Workspace.RemoteServer.RemoteEvent:FireServer()
itembutton.Text = 'Purchased'
itembutton.TextColor3 = BrickColor.Yellow().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.Yellow().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
else
itembutton.Text = 'Failed to purchase'
itembutton.TextColor3 = BrickColor.Red().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.Red().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
wait(0.1)
itembutton.Text = indextext
wait(0.5)
end
end
end)
The problem
Here is a smaller piece of code that has the same problem that your code does.
local numbers = {1, 3, 5, 6, 7}
local foundEven = false
for _, value in pairs(numbers) do
if value % 2 == 0 then
foundEven = true
print("found even", value)
else
foundEven = false
print("found odd", value)
end
end
print("final foundEven", foundEven)
If we run this code, we get this output:
found odd 1
found odd 3
found odd 5
found even 6
found odd 7
final foundEven false
A for loop does something for each. That means the rejection of 7 causes foundEven for forget that you've ever found an odd.
You don't know that you've failed until after all of the things have been checked.
A better solution
You don't need a for loop at all here. Use :FindFirstChild.
itembutton.MouseButton1Click:connect(function()
local data = game.ReplicatedStorage.PlayerData:FindFirstChild(player.Name)
assert(data, "PlayerData not created yet for `" .. player.Name .. "`")
if data.Coins.Value >=100 and (not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) then
data.Coins.Value = data.Coins.Value - amount
game.Workspace.RemoteServer.RemoteEvent:FireServer()
itembutton.Text = 'Purchased'
itembutton.TextColor3 = BrickColor.Yellow().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.Yellow().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
else
itembutton.Text = 'Failed to purchase'
itembutton.TextColor3 = BrickColor.Red().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.Red().Color
wait(0.1)
itembutton.TextColor3 = BrickColor.White().Color
wait(0.1)
itembutton.Text = indextext
wait(0.5)
end
end)
FilteringEnabled
You shouldn't be using the client to change the values. This should be done by the server. Likely the money should be subtracted (and double checked that you have enough) in the RemoteEvent.
5 comments
Because the if statement is false. (If one of the children's name is the players name, and the children's value is greater than or equal to 100, and not in the backpack, or in the character, then..blah.)
Why are you looping through all elements of the rep storage??
(not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) should have the not outside of the parentheses.
@NathanTheDeveloper You didn't read OP correctly. I stated that it FIRST goes to the else statement and THEN it goes to if it is true. Why is it doing this? I previously did not have the for i, v thing and I never had this issue, I know the statement is not false. The player has nothing in his backpack or Character and has more than enough coins but for some reason it puts "Failed to Purchase" abo
@ kingdom5 Hi, I am looking through all elements of Rep. storage because there is where I am keepving IntValues for the player's for DataStore. I was previously keeping them inside the Player but values were not being saved to the DataStore because it is set to save onPlayerRemoving and it just was not working, so I have resorted to this instead.