(UNSOLVED) Trouble with Buyable Items (Leaderboard Values)

Hello, I am working on a game and this script doesn’t seem to do what I want it to, it works like this, when you click it, it removes 2 “DNA” and unlocks a new buyable item, but it just sets the amount of “DNA” to 0.

local unclear = workspace.InfectBar
local clear = workspace.InfectBarUnclear
local obj = script.Parent.LightUp.Value
local obj2 = script.Parent.LightUp2.Value
local obj3 = script.Parent.LightUp3.Value
local cost = script.Parent.DNACost.Value
local cost2 = (script.Parent.DNACost.Value -1)


script.Parent.ClickDetector.MouseHoverEnter:Connect(function()
	clear.Transparency = 0.75
end)

script.Parent.ClickDetector.MouseHoverLeave:Connect(function()
	clear.Transparency = 1
end)

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if plr.leaderstats.DNA.Value >= cost2 then
		plr.leaderstats.DNA.Value -= cost
	unclear.Size += Vector3.new(1.1,0,0)
	unclear.Position += Vector3.new(-0.55,0,0)
	clear.Transparency = 1
	script.Parent.Decal.Color3 = Color3.new(1,0,0)
	obj.Decal.Color3 = Color3.new(1,1,1)
	obj.Handler.Disabled = false
	obj.ClickDetector.MaxActivationDistance = 99999999999
	obj2.Decal.Color3 = Color3.new(1,1,1)
	obj2.Handler.Disabled = false
	obj2.ClickDetector.MaxActivationDistance = 99999999999
	obj3.Decal.Color3 = Color3.new(1,1,1)
	obj3.Handler.Disabled = false
	obj3.ClickDetector.MaxActivationDistance = 99999999999
	script.Parent.ClickDetector:Destroy()
		script.Disabled = true
	end
end)

Video for further explanation:

(It is only supposed to get deplete only two DNA but instead depletes all DNA)

Any help is appreciated.

1 Like

Instead of “=-”,do “-=” .

“-=” will reduce the provided amount from your current total value

It sets it to 0 now (Character Limit)

local cost2 = (script.Parent.DNACost.Value +1)

what is this script parented to?

1 Like

Edit: Changed Script to an improved version of it. Still doesn’t work.
Edit: Added Video
Edit: Added more text

Why do you need to subtract the dna cost value by 1

It is for the

So, then it knows it’s over 1 resulting in 2 or more being the requirement and removes that much “DNA”

What do u mean with “remove 2 DNA”
Do you want to remove the value in leaderstats?

I’m confused why don’t you just use the cost variable instead of cost2 and check if its greater or equal to the cost, then take away the player’s dna by cost

Yes (Character Limit blah blah blah)

It sets the DNA value to 0 even with 3 DNA when it is supposed to remove 2 DNA from the player’s stats

cost2 is 1 and cost1 is 2 (Character Limit)

In my testing, the if statement triggers if the dna value is 1 even though the required cost is 2

local cost = 2
local cost2 = cost-1

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats'
	
	local DNA = Instance.new('IntValue', leaderstats)
	DNA.Name = 'DNA'
	DNA.Value = 1 -- check if the if statement works
	
	--//
	
	if DNA.Value >= cost2 then 
		DNA.Value -= cost -- -1
	end
		
	if DNA.Value >= cost then
		DNA.Value -= cost -- not triggered (as intended)
	end
end)
1 Like