Code only sometimes works

For some reason this buy button only works sometimes. Does anyone know why?
Its a serverscript by the way

local db = false
local startsize = UDim2.new(0, 113,0, 55)

local hoversize = UDim2.new(0, 120, 0, 66)
script.Parent.MouseButton1Click:Connect(function()
	local player = script.Parent.Parent.Parent.Parent.Parent.Parent
	local Cash = player.leaderstats.Cash
	if Cash.Value >= script.Parent.Parent.Cost.Value -1 and db == false then
		db = true
		local playergui = player.PlayerGui
		Cash.Value -= script.Parent.Parent.Cost.Value
		script.Parent.Parent.purchase:Play()
		local inventory = playergui.Inventory
		local found = false
		for i, v in pairs(inventory.Main:GetChildren()) do
			if v:IsA("Frame") then
				if v:FindFirstChildOfClass("TextButton").Name == script.Parent.Parent.nme.Value then
					found = true
					v.number.Value += 1
					v.Amount.Text = tostring(v.number.Value)
				end
			end
		end
		if found == false then
			print("not found creating new frame")
			local newframe = inventory.Main.Temp:Clone()
			newframe.Name = "Frame"
			newframe.number.Value = 1
			newframe.Parent = playergui.Inventory.Main
			newframe:FindFirstChildOfClass("TextButton").Name = script.Parent.Parent.nme.Value
			newframe.Called.Value = script.Parent.Parent.nme.Value
			newframe.Amount.Text = newframe.Called.Value
		end
		db = false
	else
		db = true
		script.Parent.Parent.Cantafford.Visible = true
		script.Parent.Parent.error:Play()
		wait(1)
		script.Parent.Parent.Cantafford.Visible = false
	end
end)

script.Parent.MouseEnter:Connect(function()
	script.Parent.Parent.Mouse_hover:Play()
	script.Parent:TweenSize(hoversize, Enum.EasingDirection.Out, Enum.EasingStyle.Sine, .25, true)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Parent.Mouse_hover:Play()
	script.Parent:TweenSize(startsize, Enum.EasingDirection.Out, Enum.EasingStyle.Sine, .25, true)
end)
1 Like

Sorry! I just realised I forgot to set db to false!

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