Backpack script debugging needed

I just created this Backpack system it works just fine but after I buy the tool that gives 100 points it breaks down and goes above the capacity value:
image
Script in workspace

local player = game.Players.LocalPlayer
local CapacityValue = 20

script.AddHydration.OnServerEvent:Connect(function(player)
	if player.leaderstats.Hydration.Value >= script.Capacity.Value then
		player.leaderstats.Hydration.Value = player.leaderstats.Hydration.Value - CapacityValue
		
		game.Workspace.MainEvent.Disabled = true
	else if player.leaderstats.Hydration.Value < script.Capacity.Value then
			game.Workspace.MainEvent.Disabled = false
			
		end
	end
end)

Script inside of tool

local cooldown = false

script.Parent.Activated:Connect(function()
	if cooldown == false then
		cooldown = true


		game.Workspace.Backpack.AddHydration:FireServer()
		script.Parent.Enabled = false
		wait(1)
		script.Parent.Enabled = true
		cooldown = false
	end
end)

There is a remotevent inside of Backpack script

You could add a script that says, if the item value is more than the backpacks capacity value then the player cant buy it, because they do not have enough space.

Uhm I guess you did not understand what the system does it’s buy it, it is basically that if the points value is equal to 20 then you can click anymore

Ok I’m sorry I understood what you wanted to tell,But I want the players to be free and buy anything unlike other games where they have to buy unlocked things and not locked things

Make:

If hidration.Value > capacity.Value then
hidration.Value = capacity.Value
end

But if with one click u get 100 u will surpase the capacity

Make this:

If hidration.Value > capacity.Value then
hidration.Value = capacity.Value
end

Ya in some simulator games I saw that although you have 1m points giver tool and 100 capacity it still shows 100 coins itself

Because it turns the 1 million into 100

Its what u need to do to dont surpase the 100

tried that but again it shows like this:
image
This is the scripts line

Oh ok, that seems to be a good idea.

Which one exactly please specify

If player.leaderstats.Hydration.Value > script.Capacity.Value then
Player.leaderstats.Hydration.Value = script.Capacity.Value

I’m so sorry for posting that I did not see properly, let me check

U tried this Player.leaderstats.Hydration.Value = Player.leaderstats.Hydration.Value

Use If player.leaderstats.Hydration.Value > script.Capacity.Value then
Player.leaderstats.Hydration.Value = script.Capacity.Value :slight_smile:

[quote=“setis2222, post:16, topic:1603341, full:true”]
If player.leaderstats.Hydration.Value > script.Capacity.Value then
Player.leaderstats.Hydration.Value = script.Capacity.Value
Ya I tried this but not working
:sad:

Your script should be this:

local player = game.Players.LocalPlayer
local CapacityValue = 20

script.AddHydration.OnServerEvent:Connect(function(player)
	if player.leaderstats.Hydration.Value >= script.Capacity.Value then
		player.leaderstats.Hydration.Value = script.Capacity.Value
		
		game.Workspace.MainEvent.Disabled = true
	else if player.leaderstats.Hydration.Value < script.Capacity.Value then
			game.Workspace.MainEvent.Disabled = false
			
		end
	end
end)

Does this script makes it work now?