Size >= Cost Not working

  1. What do you want to achieve? Keep it simple and clear!
    I am making one of those simple “eat and get bigger games” and I am adding a rebirth system.
  2. What is the issue? Include screenshots / videos if possible!
    So far everything has been smooth sailing but then when I was making the script to determine if the size of the player is enough to buy the rebirth, it would only give the “not enough” output when my player had more than needed.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked about everywhere and tried debugging it but it doesn’t work. Keep in mind I am not a good programmer and fairly new, and since this is my script it might be messy or not as effective.
local Button = script.Parent
local Cost = 10
local Size = game.Players.LocalPlayer.leaderstats.Size.Value

Button.MouseEnter:Connect(function()
	Button.Hover2:Play()
	Button.BackgroundColor3 = Color3.fromRGB(113, 165, 181)
	Button.Text = "Continue?"
end)
 
Button.MouseLeave:Connect(function()
	Button.Hover3:Play()
	Button.BackgroundColor3 = Color3.fromRGB(119, 230, 255)
	Button.Text = "Rebirth"
end)

Button.MouseButton1Click:Connect(function()
	if Size >= Cost then
		Button.Hover:Play()
		Button.Text = "Done!"
	elseif Size < Cost then
		Button.WRONG:Play()
		Button.Text = "Not Enough Size."
		
	end
end)

It is a local script and “buttons” are GUI.

Setting Size to leaderstats.Size.Value locks in the variable to the value at the time you assigned it.

So even as leaderstats.Size.Value changes, your variable won’t.

Try setting the Size variable to the NumberValue itself, and only access .Value inside your event handler.

1 Like

change size variable to “game.Players.LocalPlayer.leaderstats.Size” instead.
Then move to your if statement and add .Value to “Size”

1 Like

You are storing “Size’s” value inside a variable of the time script was ran and never update it further in runtime, that’s why it wouldn’t work to compare it in your script later on, cause you would compare an old value instead of a new one if it ever changes.

1 Like

The Size variable was assigned at first but never changes because it is stored as a number.

Make the Size variable point to the NumberValue, and access its value doing Size.Value

What you did does NOT point to the “Value” property of the NumberValue you wanted, it copies its value and that’s it, meaning that it’ll keep that value forever, even if you change the NumberValue.Value

1 Like

These all help so much, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.