Boolean Value Doesn't Change

I’m trying to make a shop system. The code shown below is the buy script. It’s supposed to change the value of a boolean value in a gui. When I test it the boolean value doesn’t change to true. No errors are coming up

local Cost = 100
local player = game.Players.LocalPlayer

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned.Value

script.Parent.MouseButton1Click:Connect(function()
  if playerGold.Value >= Cost and  Owned == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned = true

end
end)
1 Like

Do some checks along the way

local player = game.Players.LocalPlayer

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned

script.Parent.MouseButton1Click:Connect(function()
print("Button clicked!")
  if playerGold.Value >= Cost and  Owned == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned.Value = true
else
  print("Not enough gold!")
end
end)

Edit: Updated script with some @Wizard101fire90 of his script

1 Like

You are changing the script Variable Value, to change the bool value in the explore, do

local Owned = gui.Shop.Stats.Frame.LongStick.Owned
Owned.Value = true

It’s allowing to player to buy it, it’s just the boolvalue that’s not changing

As mentioned above, you’re changing the variable, not the value.

Yes, you need to change it the way i showed

I edited the script.
(30 chars)

Can I change it to because it starts out as false?

local Owned = gui.Shop.Stats.Frame.LongStick.Owned
Owned.Value == false

Yeah, but if you are changing it, make the two equal signs one equal sign.

When I only use one this happens

16:43:04.923 - Players.Play_MazeOfHeck.PlayerGui.ScreenGui.Shop.Stats.Frame.LongStick.Buy.Buy:7: Expected identifier when parsing expression, got '='

Send me the code.
(30 charssss)

local Cost = 100
local player = game.Players.LocalPlayer

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned.Value = false

script.Parent.MouseButton1Click:Connect(function()
  if playerGold.Value >= Cost and  Owned == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned = true

end
end)

Here is it updated.

local Cost = 100
local player = game.Players.LocalPlayer

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned
Owned.Value = false

script.Parent.MouseButton1Click:Connect(function()
  if playerGold.Value >= Cost and  Owned == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned.Value = true

end
end)

Is this supposed to be part of the line above it or a separate line?

Just replcace your entire script with what I just sent.

It wasn’t working so I added this

local Cost = 100
local player = game.Players.LocalPlayer

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned
Owned.Value = false

script.Parent.MouseButton1Click:Connect(function()
  if playerGold.Value >= Cost and  Owned == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned.Value = true
	else 
			print("Not Enough Gold")
		
	
	
end
end)

It said that I didn’t have enough money when I clearly did because I had 1 million, I then added this instead

local Cost = 100
local player = game.Players.LocalPlayer

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned
Owned.Value = false

script.Parent.MouseButton1Click:Connect(function()
  if playerGold.Value >= Cost and  Owned == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned.Value = true
	else if playerGold.Value < Cost then
			print("Not Enough Gold")
		
	end
	
end
end)

It didn’t say anything in the outputs so something else isn’t working.

This needs to be and Owned.Value == false then

Also, you can use elseif instead of creating another if statement

Thanks, it makes it buyable, but the boolvalue doesn’t change to true when you buy it.

This might help?

local Players = game:GetService("Players");

local COST = 100;
local DEBUG = true;

local goldValue = Players.LocalPlayer
    :WaitForChild("leaderstats")
    :WaitForChild("Gold");

-- Property access nightmare.
local ownedValue = Players.LocalPlayer
    :WaitForChild("PlayerGui")
    :WaitForChild("ScreenGui")
    .Shop
    .Stats
    .Frame
    .LongStick
    .Owned;

ownedValue.Value = false;

local function onClick()
    if goldValue.Value >= COST and not ownedValue.Value then
        goldValue.Value = goldValue.Value - COST;
        ownedValue.Value = true;
        if DEBUG then
            print("The item was purchased");
        end
    elseif DEBUG then
        if goldValue.Value < COST then
            local message = ("Not enough gold! Player has '%d' but needs '%d'"):format(
                goldValue.Value,
                COST
            );
            print(message);
        end
        if ownedValue.Value then
            print("Item is already owned!");
        end
    end
end

script.Parent.MouseButton1Click:Connect(onClick);

Hi
The problem might be that since you are defining the value before the click, the script is taking the initial value you gave it.

Try defining the owned variable as follows :
gui.Shop.Stats.Frame.LongStick.Owned

And then get its value within the MouseButton1Click event : Owned.Value = true.

It is weird but I think it happened to me a while ago as well and I think this fixed it.

Note : Do add a print() under the if statement to see whether it is actually checking so you know where the problem might be.