[SOLVED] Why does this function not work?

LOCAL SCRIPT:

MainFrame.StressBall.MouseButton1Click:Connect(function()
	if CanPress.Value == true then
		Equip(ShopTools["Stress Ball"], ShopTools["Stress Ball"].Name)
	else
		print("Can press = false") -- This one works
	end
end)

MainFrame.CheeseSteak.MouseButton1Click:Connect(function()
	if CanPress.Value == true then
		Equip(ShopTools["Cheese Steak"], ShopTools["Cheese Steak"].Name)
	else
		print("Can press = false") -- This one prints this even though it is set to true
	end
end)

You can see in the script what is going on. Is there a way I know why this is happening?

Any help is appreciated!

2 Likes

Could you expand on the issue? I don’t really see what’s going on

1 Like

The main purpose of this script is basically an inventory system, but it’s unrelated.

The print in the first function doesn’t print (which is good), but the second print does print (which isn’t good)

The CanPress is acting as a cooldown for how fast the player can press the button

I’ll give you more lines above:

PressedEvent.Event:Connect(function()
	if CanPress.Value == true then
		CanPress.Value = false
		task.wait(Cooldown.Value)
		CanPress.Value = true
	end
end)

MainFrame.StressBall.MouseButton1Click:Connect(function()
	if CanPress.Value == true then
		Equip(ShopTools["Stress Ball"], ShopTools["Stress Ball"].Name)
	else
		print("Can press = false") -- This one works
	end
end)

MainFrame.CheeseSteak.MouseButton1Click:Connect(function()
	if CanPress.Value == true then
		Equip(ShopTools["Cheese Steak"], ShopTools["Cheese Steak"].Name)
	else
		print("Can press = false") -- This one prints this even though it is set to true
	end
end)

Questions:

  1. Both buttons refer to the same CanPress BooleanValue object. Is this intentional?
  2. Is there ANY code that changes the value of the CanPress BooleanValue?
  3. You say the StressBall one works, does that mean it does not print “Can press = false”?
    (Oh, You just answered number 2,3 right now)
1 Like

I think it may be related to CanPress. Maybe it would be better if you made it an internal value? Try that then if it doesn’t get fixed tell me, I’ll try helping further

By the way, for your if statements, you could just do “if CanPress.Value then” instead of “== true”
(don’t think it changes anything but imo it looks better)

1 Like

Yes, they both refer to the same CanPress value, and it is intentional.

When the player presses the button, CanPress is set to false, then waits a certain amount of time, then resets back to true.

Ideas

  • If you press the stress ball button multiple times, does it still work?
  • If you press only the cheese steak button WITHOUT pressing the stress ball button before, does it still not work?

How long is the cooldown suppose to be?

1 Like

I tried doing this:

local CanPress = true

Instead of an actual value in explorer, but it still had the same outcome

Yes, the stress ball button works multiple times.

And the cheese steak button doesn’t work even before pressing it first.

Ah okay. I’m not sure how this issue is being caused…

are there any errors in the console? maybe try troubleshooting by printing the CanPress value? (I know it’ll be false but it’s worth a try)

1 Like
local CanPress = true

PressedEvent.Event:Connect(function()
	if CanPress then
		CanPress = false
		task.wait(Cooldown.Value)
		CanPress = true
	end
end)

MainFrame.StressBall.MouseButton1Click:Connect(function()
	if CanPress ~= nil then
		print("Not nil")
	else
		print("Is nil")
	end
	if CanPress then
		Equip(ShopTools["Stress Ball"], ShopTools["Stress Ball"].Name)
	else
		print("Can press = false")
	end
end)

MainFrame.CheeseSteak.MouseButton1Click:Connect(function()
	if CanPress ~= nil then
		print("Not nil")
	else
		print("Is nil")
	end
	if CanPress then
		Equip(ShopTools["Cheese Steak"], ShopTools["Cheese Steak"].Name)
	else
		print("Can press = false")
	end
end)

I did this, and they both print “Not nil”

So the cheese steak function does recognize the CanPress but doesn’t do anything about it??

1 Like

Try printing “print(CanPress)” and see what value it returns

1 Like
MainFrame.StressBall.MouseButton1Click:Connect(function()
	print(CanPress) -- prints true
	if CanPress then
		Equip(ShopTools["Stress Ball"], ShopTools["Stress Ball"].Name)
	else
		print(CanPress) -- doesnt print because it's true
		print("Can press = false")
	end
end)

MainFrame.CheeseSteak.MouseButton1Click:Connect(function()
	print(CanPress) -- prints false
	if CanPress then
		Equip(ShopTools["Cheese Steak"], ShopTools["Cheese Steak"].Name)
	else
		print(CanPress) -- prints false
		print("Can press = false")
	end
end)
1 Like

That’s very strange. Try removing the StressBall logic and see the result then.

What do you mean by that?
Could you show an example?

1 Like
--[[MainFrame.StressBall.MouseButton1Click:Connect(function()
	print(CanPress) -- prints true
	if CanPress then
		Equip(ShopTools["Stress Ball"], ShopTools["Stress Ball"].Name)
	else
		print(CanPress) -- doesnt print because it's true
		print("Can press = false")
	end
end)]]

MainFrame.CheeseSteak.MouseButton1Click:Connect(function()
	print(CanPress) -- prints false
	if CanPress then
		Equip(ShopTools["Cheese Steak"], ShopTools["Cheese Steak"].Name)
	else
		print(CanPress) -- prints false
		print("Can press = false")
	end
end)

Copy this code and replace the current code

1 Like

That was a good idea, and though something different would happen, but the same result happened. :confused:

1 Like

I’m gonna tab into this logic, clearly the code works. Show the portion of the code where your updating the CanPress value as that’s where the issue is most likely occurring

1 Like

Right above the script you saw:

PressedEvent.Event:Connect(function()
	if CanPress then
		CanPress = false
		task.wait(Cooldown.Value)
		CanPress = true
	end
end)

In every text button:

Button.MouseButton1Click:Connect(function()
	PressedEvent:Fire()
end)

I think I know why. You should put the canpress changing action inside one of the other functions. This is because you are changing the value to false before you check if it is true. Do this with your original script and not the ones they gave you.

2 Likes